943,557 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 896
  • C++ RSS
Oct 24th, 2008
0

Where is the Memory Leak ?

Expand Post »
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).
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.....
C++ Syntax (Toggle Plain Text)
  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????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Niner710 is offline Offline
31 posts
since Jul 2008
Oct 24th, 2008
0

Re: Where is the Memory Leak ?

since this is c++ why use pointers? Just make name and message both std::string and that will probably solve the memory leek problemn.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Oct 24th, 2008
0

Re: Where is the Memory Leak ?

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 25th, 2008
0

Re: Where is the Memory Leak ?

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Niner710 is offline Offline
31 posts
since Jul 2008
Oct 25th, 2008
0

Re: Where is the Memory Leak ?

your implementation doesn't work because it doesn't allocate memory for the two strings. Here is one way to implement it
C++ Syntax (Toggle Plain Text)
  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. };
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Oct 25th, 2008
1

Re: Where is the Memory Leak ?

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Object not declared in scope (didn't find answer in page)
Next Thread in C++ Forum Timeline: Addres accses violation in DLL (Borland C++ Builder 6)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC