I have a piece of code that I use to get the UNC path for the mapped drive in the CLR DLL, but when I free the memory at the end, the char array causes an invalid heap pointer statement and I assume this is due to it being allocated InteropServices, but I want to make sure that it does not turn into a memory leak, since this function is called repeatedly.
the code:
DWORD MAX_DEVICE_LENGTH = 1000;
TCHAR* szDeviceName = new TCHAR[MAX_DEVICE_LENGTH];
memset(szDeviceName, '\0', MAX_DEVICE_LENGTH);
DWORD dwResult;
char* charpath = (char*) (void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(path->Substring(0,2));
wchar_t* tpath = new wchar_t[MAX_DEVICE_LENGTH];
memset(tpath, '\0', MAX_DEVICE_LENGTH);
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, charpath, -1, NULL, 0);
MultiByteToWideChar (CP_ACP, 0, charpath, -1, tpath, dwNum );
dwResult = WNetGetConnection(
tpath,
szDeviceName, &MAX_DEVICE_LENGTH);
System::String ^ str = gcnew System::String(szDeviceName);
str += path->Substring(2, path->Length-2);
delete(szDeviceName);
free(charpath);
delete(tpath);
return str;
Perhaps there is something basic in the area of memory de-allocation, which I do not understand, but in any case, it is worth finding out. If this helps, if I skip this line, then tpath will remove the penalty, but if the charpath statement fails, then tpath will also fail.