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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top