Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link Python GC to Java GC for PythonJavaClass #59

Open
tito opened this issue Apr 26, 2013 · 1 comment
Open

Link Python GC to Java GC for PythonJavaClass #59

tito opened this issue Apr 26, 2013 · 1 comment
Labels

Comments

@tito
Copy link
Member

tito commented Apr 26, 2013

Take a look at this snippet:

class DelayedRunnable(PythonJavaClass):
    __javainterfaces__ = ['java/lang/Runnable']
    def __init__(self, callback, *args, **kwargs):
        super(DelayedRunnable, self).__init__(*args, **kwargs)
        self.callback = callback
    @java_method('()V')
    def run(self):
        self.callback()


handler = Handler(Looper.getMainLooper())
handler.postDelayed(DelayedRunnable(init_receiver), 1000)

The DelayedRunnable() instance will be deleted by Python GC, before java use it.

This is why we can have than kind of weirdness:

I/python  (25412): !! create_proxy_instance() on 1548769344 <__main__.DelayedRunnable object at 0x5c505840>
...
I/python  (25412): !! invoke0() from (1548769344, 1548769344) ('', 'system', 'dock', 'multi')
I/python  (25412):  Exception AttributeError: "'tuple' object has no attribute 'invoke'" in 'jnius.jnius.invoke0' ignored

We need to keep a reference of the DelayedRunnable until the java proxy is really deleted. Keeping a reference is not an issue, detecting when to release it is the issue.

Maybe http://docs.oracle.com/javase/6/docs/api/java/lang/ref/PhantomReference.html could help us, but i'm not sure about how to use it.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1362784-link-python-gc-to-java-gc-for-pythonjavaclass?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github).
@knappador
Copy link

If you find this page, hold a Python reference to whatever Java object is getting destroyed as a workaround. The bug -seems- very difficult, but is easy to get around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants