Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IntPtr and Marshal.FreeCoTaskMem()

Status
Not open for further replies.

csharpest

Programmer
Oct 24, 2007
1
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top