Skip to content

Instantly share code, notes, and snippets.

@kumpera
Created March 23, 2012 14:45
Show Gist options
  • Save kumpera/2171321 to your computer and use it in GitHub Desktop.
Save kumpera/2171321 to your computer and use it in GitHub Desktop.
class Foo : Java.Lang.Object { Java.Lang.Object obj; }
var foo = new Foo();
var text = new EditText ();
var foo2 = new Foo ();
//The 3 objects are reachable from the stack
foo.obj = text;
text.Click += () -> foo2.Something ();
foo2.obj = foo;
//The bridge reachability graph is:
foo -> text --- foo can reach text
text -> foo2
foo2 -> foo
foo = text = button;
GC.Collect ();
//All objects are not collectible on the C# side
We'll have the following SCC passed to the bridge callback: [foo, text, foo2]
This order is accidental and we must not rely on it.
Now it will try to the following:
add_reference (foo, text) //ok
add_reference (text, foo2) //fail
add_reference (foo2, foo) //ok
So the induced java reachability graph is:
foo -> text
foo2 -> foo
Now say the only reachable object in java land is button, both foo and foo2
will be collected and we're broken.
@jonpryor
Copy link

The bridge reachability graph is incorrect; we need to introduce more temporaries.

Instead of:

text.Click += () => foo2.Something();

it's really:

var clickHandler = new View.IOnClickListenerImplementor();
clickHandler.Handler += () => foo2.Something();
text.SetOnClickHandler(clickHandler);

Now I need to guess that the bridge reachability graph would really be:

foo -> text
text -> clickHandler
clickHandler -> EventHandler delegate -> compiler-generated type -> foo2
foo2 -> foo

I have no idea how the C# delegate mechanism interplays with the bridge code; does it just "skip" all non-Java.Lang.Object types, thus leaving clickHandler -> foo2?

Again, I'm guessing that the SCC would be [foo, text, clickHandler, foo2].

It would then try the following:

add_reference (foo, text); // OK
add_reference (text, clickHandler); // fail
add_reference (clickHandler, foo2) // OK
add_reference (foo2, foo); // ok

HOWEVER (please correct me if I'm wrong), because of the text.SetOnClickHandler() call, text DOES have a reference to clickHandler, via the View.mOnClickListener field.

I THINK that the induced Java reachability graph would thus be:

foo -> text
clickHandler -> foo2
foo2 -> foo

There would also be a direct object graph:

text -> clickHandler

Which I believe is correct, and we're fine.

@kumpera
Copy link
Author

kumpera commented Mar 23, 2012

Not really, say the SCC is [foo foo2 clickHandler text]

We'll induce the following graph on java:
foo -> foo2
foo2 -> clickHandler
clickHandler -> text

We have this direct graph:
text -> clickHandler

So, given text is the only naturally (without the induced vertices) reachable java object, both foo and foo2 would be collected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment