[Mono-list] Embedded API. Numeric Boxing and GC

Rodrigo Kumpera kumpera at gmail.com
Wed Feb 26 16:15:43 UTC 2014


This code is wrong, it will fail when the object is moved since the
gchandle is not pinning the target.

Avoid pinning as much as possible since it does hurt performance a lot.

The solution is to remove the monoObject field and
use mono_gchandle_get_target against the GC handle.


On Tue, Feb 25, 2014 at 5:20 AM, jonathan at mugginsoft.com <
jonathan at mugginsoft.com> wrote:

> I box my numeric primitives using a macro:
>
> #define DB_BOX_INT64( x ) ( mono_value_box(mono_domain_get(),
> mono_get_int64_class(), &x) )
>
>
> And use it like so:
>
> 1.
>
> - (void)box
> {
> MonoObject *monoObject = DB_BOX_INT64(value);
> }
>
> I save the MonoObject * into a local variable. The collector will see the
> object on the stack and collect it only when the enclosing function
> completes.
>
> 2.
> - (void)box:(long long)value
> {
>         self.monoObject = DB_BOX_INT64(value);
>         self.gcHandle = mono_gchandle_new(self.monoObject, FALSE);
> }
>
> - (void)dealloc
> {
>         mono_gchandle_free(self.gcHandle);
> }
>
> I save the MonoObject * into an instance variable. The collector will free
> the MonoObject after the call to mono_gchandle_free().
>
> Is the above correct?
>
> Regards
>
> Jonathan
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20140226/f99d6fcb/attachment.html>


More information about the Mono-list mailing list