问题
I have implemented a BackupAgent following the guidelines for Data Backup. The code behaves as expected until StrictMode.VmPolicy
is set to detect leaked closeable objects. After a backup is performed and GC occurs, CloseGuard reports a leaked ParcelFileDescriptor
with this stack trace:
06-28 21:47:39.683 25072-25081/com.qbix.nub E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:179)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:905)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:897)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:64)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
06-28 21:47:39.683 25072-25081/com.qbix.nub W/System.err﹕ StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
06-28 21:47:39.683 25072-25081/com.qbix.nub I/Process﹕ Sending signal. PID: 25072 SIG: 9
To confirm that I was not leaking the ParcelFileDescriptor
in my BackupAgent
, I stubbed onBackup()
like this:
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
if (oldState != null) {
oldState.close();
}
newState.close();
return;
}
This change did not fix the leak.
These are the steps I use to reproduce the problem:
- Modify app data to trigger a call to
BackupManager.dataChanged()
- Use
adb shell bmgr run
to force a backup operation - Initiate GC from
Android Studio
I don't know enough about bound services to understand if the stack trace provides clues about whether the leak is in the system BackupService
or is caused by an error in my code I am unable to see. The continued occurrence of the leak when running with the stubbed onBackup()
suggests to me that the leak is in the service.
While researching this issue, I found these other issues posted in the last few months that included the same leak report in their logcats
:
Resource leak in Android
Having problems With navigation Drawer in Android
Android - HTTP Get - Explicit Termination not called error. What am I missing?
Resolving java.lang.Throwable exception in an android
The logcat shown above is from a KitKat device. On another device running Lollipop, I think the same error is occurring. The app is killed but the logcat does not include the CloseGuard dump. Don't know what I need to do to see that.
来源:https://stackoverflow.com/questions/31108503/where-is-this-parcelfiledescriptor-leak-occurring