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).
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.
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.....
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????