[Mono-dev] PtrToStructure, FreeHGlobal, then access to the structure

Andy Hume andyhume32 at yahoo.co.uk
Tue Aug 12 04:50:30 EDT 2008

In the class library I see a few pieces of code like the following.  Am I
correct in thinking that code like this is bad?
   info = Marshal.PtrToStructure (ptr, ...);
   Marshal.FreeHGlobal (ptr);
   // now we access the structure 
   int id = info.Id;
   string name = Marshal.PtrToStringUni (info.Name);

Since the structure might contain only blittable types then the structure
reference may simply be to the native memory and not to a managed copy?  So
accessing a field there is accessing free'd memory...

With strict native heap enabled it fails, so something is wrong...


View this message in context: http://www.nabble.com/PtrToStructure%2C-FreeHGlobal%2C-then-access-to-the-structure-tp18940257p18940257.html
Sent from the Mono - Dev mailing list archive at Nabble.com.

More information about the Mono-devel-list mailing list