0

So here is what I'm trying to do:

Inline Code Example Here
my constructor takes a char pointer:

lengthString = strlen(nameSentFromMain) + 1;
namePrivate = new char[lengthString];
strcpy_s(namePrivate,lengthString,nameSentFromMain);

next in the destructor to avoid memory leaks I have:

delete [] namePrivate;

The error I'm gettting says _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). Strangely enough the destructor seems to be getting called twice for each new. Has anyone run into this before? I couldn't get a solid answer from google.

2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by mike_2000_17
0

Have you defined a copy-constructor? One that performs a deep-copy? You need to obey the rule of three when you write a class that holds a resource. You can also read this tutorial.

The double-destruction comes from the fact the your object must be getting copied, and when copied, it only copies the pointer, not the data it points to, and thus, you have two objects holding the same pointer, and when the second object is destroyed in calls delete a second time on the same pointer.

If you want to save yourself the trouble, just use the standard std::string class instead of a C-style string (char pointer).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.