[Mono-dev] Embedded API - pinning objects

Robert Jordan robertj at gmx.net
Mon Jun 29 16:53:14 EDT 2009

Steve Baer wrote:
> Hi,
> I've just started playing with embedding mono in a C++ app so I'll probably
> have plenty more questions.
> I'm a little confused about the "lifetime" of a MonoObject*. Say I create a
> MonoObject with the following code
> MonoObject* mobj = mono_object_new(domain, mclass);
> mono_object_init(mobj);
> Is mobj "pinned" in memory and if so what can I call to release the object?
> -OR-
> Do I need to immediately call mono_gchandle_new on mobj so the pointer stays
> valid while I use it?

You don't need to prevent the object from being GCed if the
object ptr is stored in a local variable, like in your sample.
The object will remain alive until the C function terminates.

In all other cases, a gchandle for the object must be allocated.


More information about the Mono-devel-list mailing list