[Mono-dev] Embedding Mono and HandleRef...

Robert Jordan robertj at gmx.net
Sun Dec 31 10:01:20 EST 2006

Thomas Wiest wrote:
> Robert Jordan wrote:
>> You don't need HandleRefs when using icalls and the embedded API.
>> Just declare the icalls as non-static and they will automatically
>> get the MonoObject* pointer of the managed object:
> Ah, very interesting. I assume this increases the ref count which 
> assures that the object won't get collected until after the native code 
> returns. Is that right?

It doesn't increase any ref count because Mono'n GC is not
based on ref counts.

The circumstance that you're able to call the icall at all,
means that the object is still alive, i.e. it's on the
current execution stack or stored in another object that is
still alive. During the icall itself the object is on the
stack (1) (it's the first argument of the icall), hence it can't
be GCed.

Since P/Invoke methods don't have the "this" parameter (they
are static), the object could be GCed during the p/invoke
call. That's why GCHandles and HandeRefs are needed.

(1) or in a processor register, but they are monitored
as well.

> Also, to pass the person struct IntPtr from C# to C, which is 
> recommended; pass it as a parameter, or use mono_runtime_invoke to pull 
> it out of the property? I would assume that passing it as a parameter is 
> quicker since it saves a function call.

It is faster to pass it as a parameter.


More information about the Mono-devel-list mailing list