Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W/System: A resource failed to call release

Tags:

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.

like image 322
Emiliho Stifler Avatar asked Jul 06 '19 05:07

Emiliho Stifler


1 Answers

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"> 
like image 193
pallgeuer Avatar answered Sep 28 '22 02:09

pallgeuer