hi,
does the call to Marshal.FreeCoTaskMem(ptr) below cause a
problem if ptr was initialized to 0 and Marshal.AllocCoTaskMem(1024) never gets called? would it dealloate memory not belonged to ptr? i could have easily avoided this "potential" problem by using a flag to indicate whether memory was allocated. but i just want to know...
void testFree()
{
IntPtr ptr = (IntPtr) 0; // initialization required
try
{
... // code that may throw an exception
ptr = Marshal.AllocCoTaskMem(1024);
... // code that may throw an exception
}
catch
{
}
finally
{
// FreeCoTaskMem() does nothing if ptr is null
Marshal.FreeCoTaskMem(ptr);
}
}
thanks
does the call to Marshal.FreeCoTaskMem(ptr) below cause a
problem if ptr was initialized to 0 and Marshal.AllocCoTaskMem(1024) never gets called? would it dealloate memory not belonged to ptr? i could have easily avoided this "potential" problem by using a flag to indicate whether memory was allocated. but i just want to know...
void testFree()
{
IntPtr ptr = (IntPtr) 0; // initialization required
try
{
... // code that may throw an exception
ptr = Marshal.AllocCoTaskMem(1024);
... // code that may throw an exception
}
catch
{
}
finally
{
// FreeCoTaskMem() does nothing if ptr is null
Marshal.FreeCoTaskMem(ptr);
}
}
thanks