Heap corruption detected error when attempting to free pointer
Posted
by
AndyGeek
on Stack Overflow
See other posts from Stack Overflow
or by AndyGeek
Published on 2010-12-24T19:49:31Z
Indexed on
2010/12/24
19:54 UTC
Read the original article
Hit count: 307
c++
|memory-management
Hi,
I'm pretty new to C++ and have run into a problem which I have not been able to solve. I'm trying to convert a System::String to a wchar_t pointer that I can keep for longer than the scope of the function. Once I'm finished with it, I want to clean it up properly. Here is my code:
static wchar_t* g_msg;
int TestConvert()
{
pin_ptr<const wchar_t> wchptr = PtrToStringChars("Test");
g_msg = (wchar_t*)realloc(g_msg, wcslen(wchptr) + 1);
wcscpy(g_msg, wchptr);
free (g_msg); // Will be called from a different method
}
When the free is called, I'm getting "HEAP CORRUPTION DETECTED: after Normal block (#137) at 0x02198F90."
Why would I be getting this error?
Andrew L
© Stack Overflow or respective owner