7 Replies - 1709 Views - Last Post: 31 July 2009 - 01:44 PM Rate Topic: -----

#1 charlweed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-March 09

Why does delete [] crash in my destructor?

Posted 31 July 2009 - 08:48 AM

I have a member variable
wchar_t * _message;

This array is allocated in the constructor,
_message = new wchar_t [messageLength];

But when I try to free the memory with delete in my destructor, it crashes with a a HEAP CORRUPTION error.
class ExampleZero
{
public:
ExampleZero( wchar_t* cppMessage);
~ExampleZero(void);
private:
const wchar_t *_message;
};

ExampleZero::ExampleZero( wchar_t* cppMessage)
{
_message = NULL;
errno_t winError;
if (cppMessage == NULL){
fwprintf(stderr, L"Null pointer for string.\n");
return;
}
size_t messageLength = wcslen(cppMessage);
if (messageLength < 1){
fwprintf(stderr, L"string is empty or invalid.\n");
return;
}
try{
long address=NULL;
_message = new wchar_t [messageLength];
address = (long) _message;
fprintf(stderr,"%x",address);
}
catch(std::exception& e){
fprintf(stderr, e.what());
return;
}
if(_message == NULL){
return;
}
SecureZeroMemory((PVOID)_message,messageLength);

winError = wcscpy_s((wchar_t *)_message, messageLength+1, cppMessage);

}

ExampleZero::~ExampleZero(void)
{
if(_message != NULL){
try{
/* Why does it crash here? */
delete [] _message;
}
catch(std::exception& e){
fprintf(stderr, e.what());
}
_message=NULL;
}
}

/*******************/
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{

ExampleZero * example0;
example0 = new ExampleZero(L"I Crash");
delete example0;
}

return nRetCode;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why does delete [] crash in my destructor?

#2 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 09:12 AM

Hello, :D

You have a very nice program.

Some times changing things around helps. Take a look at the following and see if it has to be private. Try it in public and see if it makes a difference.

private:
const wchar_t *_message;



If it does make a difference, figure out why make necessary changes and put it back in public if that is where you really want it. :D
Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 09:25 AM

Hello,

Another thing to try is that you might need to match your new with delete. Your new does not have the [], try the delete without the [] and see if that helps. :D
Was This Post Helpful? 0
  • +
  • -

#4 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 09:36 AM

Hello, :D

Sorry, I am going blind. I see the [] in the new now, sorry.

Another possibility to consider is you might need a pointer; for example:

delete [] pointer;

You have

delete [] _message;
:D
Was This Post Helpful? 0
  • +
  • -

#5 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 09:56 AM

Hello again, :D

OK, I am going blind, wchar_t is a wide character type.

You declared *_message to be a pointer pointing to a wide character type.

Then you initialized the pointer _message = NULL;

Then you assigned _message = new wchar_t [messageLength];

The & is not required so I am assuming this last line means you assigned _message new wide character memory of size [messageLength].

Therefore your delete [] _message; should have worked; unless we have a scope issue.

Did you try to delete memory that was out of scope?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 10:32 AM

Why is your _message member a constant?
Was This Post Helpful? 0
  • +
  • -

#7 charlweed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-March 09

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 11:01 AM

Solved.
The problem is that wcslen ignores the null terminator, while wcscpy_s expects one.

wcslen(cppMessage) 
does not count the null character at the end of cppMessage.
wcscpy_s((wchar_t *)_message, messageLength, cppMessage)
assumes there will be a null character at the end of cppMessage.

So I change
size_t messageLength = wcslen(cppMessage);

to
size_t messageLength = wcslen(cppMessage)+1;


and
winError = wcscpy_s((wchar_t *)_message+1, messageLength+1, cppMessage);

to
winError = wcscpy_s((wchar_t *)_message, messageLength, cppMessage);


And then in all the various other calls in my real code, use messageLength-1 to avoid inserting an extraneous null character into my string.

Yuck yuck yuck.
Was This Post Helpful? 0
  • +
  • -

#8 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Why does delete [] crash in my destructor?

Posted 31 July 2009 - 01:44 PM

using safe methodes is complementary in fault tolerant designs,

This post has been edited by Plus: 31 July 2009 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1