On 25 Feb 2014, at 12:18, Robert Jordan <[email protected]> wrote:

> On 25.02.2014 11:20, [email protected] 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
_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to