| | |
Where is the Memory Leak ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 25
Reputation:
Solved Threads: 0
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).
Here is my cpp file.
In my main file I have.....
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????
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)
Test.h //header file class StringValues { public: StringValues(); ~StringValues(); char *Name; char *Message; } class Tblk { public: Tblk(); ~Tblk(); private: StringValues myStringValues; }
Here is my cpp file.
C++ Syntax (Toggle Plain Text)
Tblk::Tblk() { //constructor } Tblk::~Tblk { //destructor } Tblk::Tblk(const Tblk& other) { strncpy(myStringValues.Name, other.myStringValues.Name, 100); strncpy(myStringValues.Message, other.myStringValues.Message, 100); } StringValues::StringValues() { Name = new char[MAXCOMMENTSTRING]; Message = new char[MAXCOMMENTSTRING]; } StringValues::~StringValues() { delete [] Name; ===Error here delete [] Message; }
In my main file I have.....
C++ Syntax (Toggle Plain Text)
void main() { Tblk A; Tblk B = A; }
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????
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
I think what is happening is this
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.
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.
•
•
Join Date: Jul 2008
Posts: 25
Reputation:
Solved Threads: 0
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)
Tblk::Tblk(const Tblk& other) { strncpy(myStringValues.Name, other.myStringValues.Name, 100); strncpy(myStringValues.Message, other.myStringValues.Message, 100); }
Last edited by Niner710; Oct 25th, 2008 at 5:19 am.
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)
const int MAXCOMMENTSTRING = 255; class StringValues { public: StringValues() { Initialize(); } StringValues(StringValues& s) { Initialize(); strcpy(Name,s.Name); strcpy(Message,s.Message); } ~StringValues() { delete[] Name; delete[] Message; } char *Name; char *Message; private: void Initialize() { Name = new char[MAXCOMMENTSTRING]; Message = new char[MAXCOMMENTSTRING]; memset(Name,0,MAXCOMMENTSTRING); // optional memset(Message,0,MAXCOMMENTSTRING); // optional } };
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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)
//add this to the public part of the header Tblk & operator=(Tblk const &); //implement it in the .cpp like this: Tblk & Tblk::operator=(Tblk const & rhs) { strncpy(myStringValues.Name, rhs.myStringValues.Name, 100); strncpy(myStringValues.Message, rhs.myStringValues.Message, 100); return *this; }
Last edited by skatamatic; Oct 25th, 2008 at 11:33 pm.
![]() |
Similar Threads
- Memory leak in game (C++)
- SDL library memory leak (C)
- Looking for Memory Leak Advice (C++)
- Can't prevent memory leak (C++)
- can't find my memory leak (C++)
- Memory Leak? (C)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C++ Forum
- Previous Thread: Object not declared in scope (didn't find answer in page)
- Next Thread: Addres accses violation in DLL (Borland C++ Builder 6)
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






