Please help, Heap corruption error recieved

Hi guys,

I am trying to implement decorater design pattern. But I found my program has memory leak. so I modified it but now It has stopped working. please can anyone help what I am doing wrong.

Please find the code attached below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
using namespace std;

class Computer
{
public:
	Computer()
	{
	}
	virtual char* description()
	{
		return("Computer");
	}
	virtual ~Computer()
	{
		cout<<"In comp dtor"<<endl;
	}
};

class ComponentDecorator: public Computer
{
public:
	char* description() = 0;
};

class Disk: public ComponentDecorator
{
	Computer *c;
	char *s;
public:
	Disk(Computer *tc)
	{
		c=tc;	
		s=NULL;
	}
	char* description()
	{
		int n;
		if(s != NULL)
			delete s;
		n =  strlen(c->description());
		s = new char[n+10];		
		s[0] = '\0';
		strcat(s,c->description());
		strcat(s," and Disk ");
		return(s);
	}
	~Disk()
	{
		if(s!=NULL)
			delete s;
		delete c;
		cout<<"In Disk dtor"<<endl;
	}
};

class Monitor: public ComponentDecorator
{
	Computer *c;
	char *s;
public:
	Monitor(Computer *tc)
	{
		c=tc;
		s=NULL;
	}
	char* description()
	{
		int n;
		if(s != NULL)
			delete s;
		n =  strlen(c->description());
		s = new char[n+13];
		s[0] = '\0';
		strcat(s,c->description());
		strcat(s," and Monitor ");
		return(s);
	}
	~Monitor()
	{
		if(s!=NULL)
			delete s;
		delete c;		
		cout<<"In Monitor dtor"<<endl;
	}
};

void main()
{
	Computer *comp = new Computer();
	//cout<<comp->description()<<"\n";
	
	comp = new Disk(comp);
	//cout<<comp->description()<<"\n";

	comp = new Monitor(comp);
	cout<<comp->description()<<"\n";
	
	delete comp;
	getchar();
}
I had to add a missing #include <cstring> , correct your void main() to int main() and remove that getchar();, but it does compile.

You are writing to memory that is not yours in the following places:

Line 46
Line 77

You subsequently read from the same memory that is not yours, and you have some mismatched delete.

If you run your code nuder the free tool valgrind, it will tell you all of this.
Last edited on
Thanks buddy, but if I remove the lines 40,41 and 71,72 the program works. but there are memory leaks. Please can you tell me a way by which I can avoid those memory leaks.

Thanks for the help buddy.
The lines 46/77 are not the problem (ok they are somewhat) and removing 40/41 and 71/72 is not the solution.

The problem is on line 43/74. You need to +1 for the trailing 0. And (less important) delete[] on line 41/72

EDIT: Remove line 53/84 since that are objects you haven't created. So you delete 'com' created on line 91 three times.
Last edited on
hey thanks buddy,

I tried removing line 53/84, but it gave me memory leaks... I am using visual studio 2008. But after your solution for 43/74. It worked. thank you guys. :)
Topic archived. No new replies allowed.