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

jonathan at mugginsoft.com jonathan at mugginsoft.com
Tue Feb 25 13:33:21 UTC 2014


On 25 Feb 2014, at 12:18, Robert Jordan <robertj at gmx.net> wrote:

> On 25.02.2014 11:20, jonathan at mugginsoft.com wrote:
>> 
>> 
>> Is the above correct?
> 
> Not quite. Look at this line:
> 
> self.monoObject = DB_BOX_INT64(value);
> 
> There is a chance that the MonoObject* remains only reachable
> from self.monoObject. This means that it might be GCed before
> a handle could be taken from it.

That’s a good point.
In my actual implementation the monoObject property uses the following setter function, which I think gets it right.

So self.monoObject = DB_BOX_INT64(value) is equivalent to [self setMonoObject:DB_BOX_INT64(value)];
_ preceeds a class instance variable.

-(void)setMonoObject:(MonoObject *)monoObject
{
    if (_monoObject) {
        mono_gchandle_free(_gcHandle);
        _gcHandle = 0;
    }
    _monoObject = monoObject;
    _gcHandle = mono_gchandle_new(_monoObject, FALSE);
}

> 
> For the paranoid:
> 
> self.gcHandle = mono_gchandle_new(DB_BOX_INT64(value), FALSE);
> self.monoObject = mono_gchandle_get_target(self.gcHandle);
> 
Are there any actual situations where this is advised?
If the collector is doing its stuff it shouldn’t ever be necessary.

Jonathan


More information about the Mono-list mailing list