[Mono-dev] boehm and pin to sgen and moveable

Robert Jordan robertj at gmx.net
Sun Oct 4 18:02:24 UTC 2015

Hey Jonathan,

On 03.10.2015 16:25, Jonathan Mitchell wrote:
> In my ObjC->Mono bridge I have been using the Boehm collector and pinned memory (as an expedient).
> I now want to move to SGEN and moveable memory.
> As a first step I have switched to SGEN and left pinning enabled.
> My ObjC code wraps a MonoObject * in an NSObject.
> When the MonoObject is set it is pinned.
> A simple monoObjectTrace property watches to see if the MonoObject moves.
> Under the Boehm collector objects seem to stay pinned as requested.
> Under SGEN they don’t i.e.: the exception in the second method below raises i.e.:  self.monoObjectTrace != (NSUInteger)monoObject)
> What am I doing wrong?

It depends on how your methods are actually used.

In setMonoObject, there is a time frame between

mono_gchandle_free ()
mono_gchandle_new ()

where the GC might regain control and move the object.

Try to ensure that setMonoObject is called only once for
the same obj, such that mono_gchandle_free () followed
by mono_gchandle_new () isn't needed.


More information about the Mono-devel-list mailing list