Resource leak in Android

空扰寡人 提交于 2019-12-25 01:46:20

问题


I have been developing an android app for a few weeks now and keep seeing this problem every now and again:

06-24 14:04:48.915    1530-1539/android.process.acore 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:180)
        at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
        at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
        at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)
        at android.os.Binder.execTransact(Binder.java:446)

The only thing I can think of that is causing this is that I open a url in the browser using this code:

ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.practiceadmin.com/legal/"));
        browserIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(browserIntent);

    }
};

The strange thing is when I open up my android.os.ParcelFileDescriptor.java file, it says that for the import statements:

import dalvik.system.CloseGuard;
import libcore.io.IoUtils;
import libcore.io.Memory;

it cannot resolve symbol 'CloseGaurd'/'libcore'. Any direction in solving this would be greatly appreciated.


回答1:


This is only a report of my research on the issue--not an answer yet.

This problem has been reported recently in related posts: for example, here, here and here.

I have an app with an enabled BackupAgent. When I recently enabled StrictMode, I started seeing the same CloseGuard exception included in this and the other posts, which includes IBackupAgent in the stack trace. So I assumed I had a problem in my BackupAgent.

I saw I had neglected to close the ParcelFileDesciptors in my BackupAgent's onBackup() method, so added these statements:

// if there is no prior state, oldState is null
if (oldState != null) {
    oldState.close();
}
newState.close();

The random exceptions continued even with this change. I then noticed that in some of the other reports of the issue, the OPs were new Android developers and probably hadn't yet created BackupAgents for their app. This leads me to suspect that the resource leak is in the framework code for the BackupService, or is caused by BackupAgents in other apps. The logcat for this crash shows that the DictionaryBackupAgent was active.

I don't have a good understanding of how far StrickMode settings for an app component are propagated. The StrictMode documentation indicates that "it does propagate its state across process boundaries when doing Binder calls". This suggests that an app's StrictMode settings may be larger than expected. These crashes may be occurring anytime StrictMode is enabled and a backup occurs, and may not be caused by the code of the app in which they are reported.



来源:https://stackoverflow.com/questions/31034193/resource-leak-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!