如何释放非托管代码的内存
有个问题比较困惑,非托管c++中有如下代码;
INF_APP bool GetTestDateTime(char** TestDateTime)
{
char source[100] = "ceshishuzhu";
char* pstr= new char[100];
memcpy_s(pstr,100,source,100);
*TestDateTime = pstr;
return true;
}
在C#中调用方式如下:
[DllImport("InfParser.dll", EntryPoint = "GetTestDateTime", CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetTestDateTime(ref IntPtr TestDateTime);
String testtime = string.Empty;
IntPtr ptr = new IntPtr();
bool bRet = NativeMethod.GetTestDateTime(ref ptr);
testtime = Marshal.PtrToStringAnsi(ptr);
程序运行,没问题,可是c++函数中new的内存没有机会去释放,会有内存泄漏。请问这种情况如何释放那个临时分配的内存呢?