Use of Marshal.FreeCoTaskMem after Marshal.PtrToStructure

  • Thread starter Thread starter Ken Allen
  • Start date Start date
K

Ken Allen

The documentation is not clear on the exact behaviour of the
Marshal.PtrToStructure method and whether it copies the contents of the
IntPtr region to a new managed object or whether it creates the managed
object to reference the same region of memory.

Question 1: should I call Marshal.FreeCoTaskMem after calling
Marshal.PtrToStructure?

Question 2: the Marshal.PtrToStructure method seems to create an object for
the structure -- should I be concerned with converting it to a value object?

-Ken
 
Ken,
whether it copies the contents of the
IntPtr region to a new managed object or whether it creates the managed
object to reference the same region of memory.

It creates a new object. All managed objects have to be allocated from
the managed heap.

Question 1: should I call Marshal.FreeCoTaskMem after calling
Marshal.PtrToStructure?

Most likely not. But it depend on who allocated the memory to begin
with and where you get the pointer from.

Question 2: the Marshal.PtrToStructure method seems to create an object for
the structure -- should I be concerned with converting it to a value object?

Not sure what you mean here.



Mattias
 
Back
Top