views:

128

answers:

1

I've got a managed c++ library that is crashing when a delete statement is encountered, but it only crashes on a 64-bit build. The code is:

LPWSTR pwmsURL = NULL;
pPresentationCtx->GetStringValue(L"WMS_PRESENT_ORIGINAL_REQUEST_NAME", 17, &pwmsURL, 0);
String^ wmsURL = gcnew String(pwmsURL);
//this delete is the problem line...
delete [] pwmsURL;

If I comment out the delete, everything works fine, but I don't want to create a memory leak. No problems on a 32-bit build. Any idea what's going on with this?

+2  A: 

delete[] can only delete memory allocated by new[]. To use it here, you'd need to know exactly how pPresentationCtx->GetStringValue allocated the returned memory, and it's seemingly not via new[] - hence the crash.

I would imagine that the API you're using provides a function specifically for deallocating memory created by its provided functions. Check the documentation (could be something like GlobalFree if this is Win32).

Adam Wright
Thanks - went back and dug into the docs, it was indeed using CoTaskMemAlloc/CoTaskMemFree .
flatline