Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

java - W/System: A resource failed to call release

As the title says. When using the console of AndroidStudio on my app it shows:

W/System: A resource failed to call release.

Sometimes it is said multiple times. I know what it means but I've checked the almost 2k lines of codes multiple times but I'm clueless what I'm missing to close/release.

Is there any way to expand on this information from the console? or how would you do to target what the resource is or when it fails to close? Any ideas are welcome. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer from @guest works, but you can achieve the exact same thing without resorting to reflection using Strict Mode. Specifically, something like:

StrictMode.setVmPolicy(new VmPolicy.Builder()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .build());

In general, strict mode can do much more for you though (see link above to doc), and all you need to do for a default setup is:

StrictMode.enableDefaults();  # <-- This includes warning on leaked closeables

To enable strict mode "as soon as possible" you can add either of the above code options to the constructor of your application class, e.g.:

public class MyApp extends Application {

    public MyApp() {
        if(BuildConfig.DEBUG)
            StrictMode.enableDefaults();
    }

}

Note that in addition to just creating the class above, you need to tell Android that you have created a custom application class in the AndroidManifest.xml (so that an instance of it gets created when your application process starts, instead of Android creating the default Application class). You need to add/modify the android:name attribute of the <application> tag to point to the fully resolved package path of your custom application class (MyApp in this case):

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name="com.example.app.MyApp"  <-- IMPORTANT PART: ADAPT FOR YOUR ACTUAL PROJECT
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...