Search code examples
c++delete-operator

Deleting TCHAR pointer


I am trying to do make a C++ hello world that supports Unicode, but I am a little stuck.

I made a pointer to a TCHAR [I think its a char array], and after using it, I attempt to delete it. It crashes saying Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse).

Checking the interwebs, someone said it was because the wrong delete was used. I tried both deletes, but it still gives the same message.

Did I miss something obvious?

Code I tried:

TCHAR *str=TEXT("おはよう, World!");
delete[] str;

Also tried:

TCHAR *str=TEXT("おはよう, World!");
delete str;

Solution

  • TCHAR *str=TEXT("おはよう, World!");
    

    You're not allocating anything, so there's no need to delete the memory. Simply don't call delete[]. TEXT is a macro, not a function returning some memory you're supposed to manage yourself.

    It's like calling delete after

    char* x = "bla";
    delete[] x;
    

    Just illegal, since you don't own the memory.