In Visual Studio the follow C code causes
HEAP CORRUPTION DETECED exception at line free(x);
Why? and how do I fix it?
LPWSTR x=(LPWSTR) malloc( TCHAR(lstrlen(storeName) * sizeof(TCHAR)));
LPCWSTR storeName = (LPCWSTR)L"xxxx";
wcscpy_s(x, lstrlen(storeName) * sizeof(TCHAR), storeName);
free(x);
In the debugger, I can see buffer x, after line
wcscpy_s(aStorePtr->storeName, lstrlen(storeName) * sizeof(TCHAR), storeName);
(a TCHAR buffer) is filled with ‘xxxx’
the local string value,
LPCWSTR storeName = (LPCWSTR)L"xxxx";
on the stack,
so I expect to be able to free the buffer.
New contributor
John Rainey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.