Marshal destroy/free

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

If allocating a structure like this...

SturctPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStructure));
Marshal.StructureToPtr(myStructure,SturctPtr ,false);

Do I need to call both free methods below or does DestroyStructure() free
the structure pointer itself? I am not sure why StructureToPtr() does not
allocate the memory for the structure there. It doesn't seem right that
both methods need to be called. Can someone please verify this for me. The
MSDN help is not to clear on this.

Marshal.DestroyStructure(SturctPtr ,typeof(MyStructure));
Marshal.FreeHGlobal(SturctPtr );
 
Bill,
Do I need to call both free methods below or does DestroyStructure() free
the structure pointer itself?

You need to call FreeHGlobal, but probably not DestroyStructure.



Mattias
 
Back
Top