Re: [android-developers] AIDL callbacks leak memory?
The underlying binder mechanisms keeps track of references across objects. Native IBinder objects are reference counted; the framework holds a reference on them as long as remote processes hold references. The JNI wrapper holds a reference on the Java object as long as the native binder object is alive.
I've been digging into Android IPC, recently and there's something puzzling me.Suppose I define a Parcelable that, as one of its arguments, takes a callback listener which is also Parcelable.Like this:
oneway interface MyService {
void start(in MyRequest req, in MyResponseListener listener);
}
oneway interface MyResponseListener {
void onResponse(in MyResponse response);
}In my code, I'm going to do something like this:
listener = new MyResponseListener() { /*... */ }--boundService.start(req, listener);
I think this is pretty much how Cursors work. I see one of two possibilites here:
1) the call to start() doesn't create an additional reference to the listener object. That means that, to prevent its being gc'ed, my code must keep a reference to the listener, somewhere, until it ether gets the callback or doesn't want the it anymore. (btw, what happens if the listener is gone when the callback happens?)
2) the call to start() creates a new, GC-visible reference to listener. I can pass an anonymous class without fear that it will be GCed. On the other hand, it and everything to which it holds references are leaked until that reference gets nulled. When does that happen?
I've followed this down as far as a Parcel.flatten_binder, but I kind of lose it after that.
What is the right way to keep from leaking, for instance, implicit pointers to my Activity, when I use asynchronous callbacks with a bound Service?
Thanks
The second edition of Programming Android is now on-line:
-blake
G. Blake Meike
Marakana
http://shop.oreilly.com/product/0636920023005.do
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Dianne Hackborn
Android framework engineer
hackbod@android.com
Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them.
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home