Where is the Memory Leak ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 25
Reputation: Niner710 is an unknown quantity at this point 
Solved Threads: 0
Niner710 Niner710 is offline Offline
Light Poster

Where is the Memory Leak ?

 
0
  #1
Oct 24th, 2008
Hi,

I keep getting an error in Visual Studio C++. I am trying to create a copy constructor but keep getting this error.

"Windows has triggered a breakpoint in test.exe.
This may be due to a corruption of the heap, and indicates a bug in test.exe or any of the DLLs it has loaded."

After going thru the debugger I found the problem on a delete [] that I do.

Here is a simplified version of my code. I have a couple classes in my header file(I know this is bad design but for now I just want to understand what is going wrong).
  1. Test.h //header file
  2. class StringValues
  3. {
  4. public:
  5. StringValues();
  6. ~StringValues();
  7. char *Name;
  8. char *Message;
  9. }
  10.  
  11. class Tblk
  12. {
  13. public:
  14. Tblk();
  15. ~Tblk();
  16. private:
  17. StringValues myStringValues;
  18. }

Here is my cpp file.
  1. Tblk::Tblk()
  2. {
  3. //constructor
  4. }
  5.  
  6. Tblk::~Tblk
  7. {
  8. //destructor
  9. }
  10.  
  11. Tblk::Tblk(const Tblk& other)
  12. {
  13. strncpy(myStringValues.Name, other.myStringValues.Name, 100);
  14. strncpy(myStringValues.Message, other.myStringValues.Message, 100);
  15. }
  16.  
  17. StringValues::StringValues()
  18. {
  19. Name = new char[MAXCOMMENTSTRING];
  20. Message = new char[MAXCOMMENTSTRING];
  21. }
  22.  
  23. StringValues::~StringValues()
  24. {
  25. delete [] Name; ===Error here
  26. delete [] Message;
  27. }

In my main file I have.....
  1. void main()
  2. {
  3. Tblk A;
  4. Tblk B = A;
  5. }

When I run the code thru the debugger object B gets destroyed and then myStringValues destructor is called. This is where I get an error. When I get to "delete [] Name" I get the error message that there may be a corruption of the heap. This seems like it should work. I allocated dynamic memory on the heap in the StringValues constructor and delete it in the destructor. Can anyone tell me why this doesn't work????
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Where is the Memory Leak ?

 
0
  #2
Oct 24th, 2008
since this is c++ why use pointers? Just make name and message both std::string and that will probably solve the memory leek problemn.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Where is the Memory Leak ?

 
0
  #3
Oct 24th, 2008
I think what is happening is this

Tblk A;
 Tblk B = A;

This is using a default copy constructor since you haven't defined one, and both your objects A and B are pointing to the same set of strings.

So when you leave main, both A and B will be destroyed, but once you destroy A and delete the pointers, when B's destructor is called, you have no valid pointer values to destroy. Hope that makes sense.

And as Ancient Dragon said you should use strings, and declare a copy constructor explicitly in order to avoid such situations.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 25
Reputation: Niner710 is an unknown quantity at this point 
Solved Threads: 0
Niner710 Niner710 is offline Offline
Light Poster

Re: Where is the Memory Leak ?

 
0
  #4
Oct 25th, 2008
I actually created a copy constructor, so I wouldn't have that exact problem that you mentioned. I dont know if the issue is with the copy constructor that I used. I probably should just try this by using the string class, but am just curious for further knowledge why my implementation won't work. This was the copy constructor that I had.

  1. Tblk::Tblk(const Tblk& other)
  2. {
  3. strncpy(myStringValues.Name, other.myStringValues.Name, 100);
  4. strncpy(myStringValues.Message, other.myStringValues.Message, 100);
  5. }
Last edited by Niner710; Oct 25th, 2008 at 5:19 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Where is the Memory Leak ?

 
0
  #5
Oct 25th, 2008
your implementation doesn't work because it doesn't allocate memory for the two strings. Here is one way to implement it
  1. const int MAXCOMMENTSTRING = 255;
  2. class StringValues
  3. {
  4. public:
  5. StringValues()
  6. {
  7. Initialize();
  8. }
  9. StringValues(StringValues& s)
  10. {
  11. Initialize();
  12. strcpy(Name,s.Name);
  13. strcpy(Message,s.Message);
  14. }
  15. ~StringValues()
  16. {
  17. delete[] Name;
  18. delete[] Message;
  19. }
  20. char *Name;
  21. char *Message;
  22. private:
  23. void Initialize()
  24. {
  25. Name = new char[MAXCOMMENTSTRING];
  26. Message = new char[MAXCOMMENTSTRING];
  27. memset(Name,0,MAXCOMMENTSTRING); // optional
  28. memset(Message,0,MAXCOMMENTSTRING); // optional
  29. }
  30. };
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Where is the Memory Leak ?

 
1
  #6
Oct 25th, 2008
When you leverage off of the = operator without an overloaded ooperator=() implemented, the compiler uses a predefined method of copying the data by doing a direct member to member copy. Since you are using pointers as members, the addresses of the pointers are being copied, rather than firing the copy constructor that you have written. The easiest way to fix this would be to implement an overloaded = operator like this:

  1. //add this to the public part of the header
  2. Tblk & operator=(Tblk const &);
  3.  
  4. //implement it in the .cpp like this:
  5. Tblk & Tblk::operator=(Tblk const & rhs)
  6. {
  7. strncpy(myStringValues.Name, rhs.myStringValues.Name, 100);
  8. strncpy(myStringValues.Message, rhs.myStringValues.Message, 100);
  9. return *this;
  10. }
Last edited by skatamatic; Oct 25th, 2008 at 11:33 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC