Re: Safe handles and Marshal class

Alex Ivanoff <[email protected]> Tue, 12 Feb 2008 14:44:33 -0600
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
This is the pattern I use today:

IntPtr buffer = IntPtr.Zero;
try
{
  buffer = Marshal.AllocHGlobal(size);
  ...
}
finally
{
  if (buffer != IntPtr.Zero)
  {
    Marshal.FreeHGLobal(buffer);
  }
}

And this is what I am after:

using (SafeMemoryHandle buffer = Marshal.SafeAllocHGlobal())
{
  ...
}

Where SafeMemoryHandle is SafeHandle-derived class.


-----Original Message-----
From: Discussion of development on the .NET platform using any managed language [mailto:[email protected]] On Behalf Of Peter Ritchie
Sent: Tuesday, February 12, 2008 13:39
To: [email protected]
Subject: Re: [DOTNET-CLR] Safe handles and Marshal class

In what context?  Are you looking for something to
automatically/deterministically free native memory that you've manually
allocated?

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com