On Sep 15, 2011, at 7:52 AM, Martyn Wendon wrote:
> I think that this turned out to be an incorrect (?) use of the "using" 
> statement.....

I believe you are correct.

> What I was doing was (pseudo code):
> 
>       AbsoluteLayout al = new AbsoluteLayout(this);
> 
>       using (RotatingTextView rtv = new RotatingTextView())
>       {
>               rtv.somenewproperty = somevalue;
>               al.AddView(rtv);
>       }
> 
>       SetContentView(al);

Yeah, that's bad. :-)

As mentioned elsewhere, for every Java.Lang.Object subclass which is created in 
managed code, a Java side object is created. Internally, we store a mapping 
between the Java object's JNI handle and the managed object reference value:

        Dictionary<IntPtr /* JNI handle */, WeakReference /* managed mapping 
*/> instances;

Now, calling Java.Lang.Object.Dispose() _removes_ the JNI handle <-> object 
mapping. This allows the Java GC to (eventually) collect the Java-side object, 
and for Mono's GC to (eventually) collect the managed object; otherwise, both 
objects will be kept alive.

When you use the `using` block as you do, you're removing the JNI mapping, 
disassociating the Java object from the managed object.

When an overridden method is eventually invoked on the Java-side 
RotatingTextView instance, Mono for Android will consult the instance mapping, 
see that one doesn't exist, and _create_ a new managed object. This is why your 
fields were always 0 -- it was a different object instance, mapped to the same 
Java instance.

> I removed the "using" statement and it all started working as it should.

Yup, because the instance mapping is preserved. :-)

> Unless you think that this is still a bug, I won't file a bug report!!

Yes, this isn't a bug.

Thanks,
 - Jon

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to