• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Chasing origin of error A resource failed to call close

I've spend a day and an half now trying to chase the origin of a LeakedCloseableViolation in my app, developed to target API v32.

I have enabled strict mode with:
Code:
StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder()
  .detectLeakedClosableObjects()
  .penaltyListener( this.getMainExecutor(), ( Violation v ) -> {
   v.fillInStackTrace();
   v.printStackTrace();
  } )
  .build());
And in LogCat I get
Code:
W/System.err: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close
W/System.err:     at com.wattthis.reflowmate.App.lambda$onCreate$0(App.java:30)
W/System.err:     at com.wattthis.reflowmate.App$$ExternalSyntheticLambda0.onVmViolation(Unknown Source:0)
W/System.err:     at android.os.StrictMode.lambda$onVmPolicyViolation$3(StrictMode.java:2462)
W/System.err:     at android.os.StrictMode$$ExternalSyntheticLambda3.run(Unknown Source:4)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:942)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loopOnce(Looper.java:201)
W/System.err:     at android.os.Looper.loop(Looper.java:288)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7898)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Stacktrace doesn't appear to have any cause listed. I tried added getCause() to Violation that is thrown, but it returns Null.

I audited my code, and don't see any streams or observers, that are not being properly closed.

The error is perfectly reproducible.
I just open the app, and leave it open on the main Activity for 30 seconds. I don't touch the phone at all.
After 30 seconds, when garbage collection comes around, this error appear.

Anyone have any idea on how I can get more information about what is causing this issue?
 
hi
To help you troubleshoot the issue you are experiencing, it might be helpful to try the following steps:

  1. Review the code around line 30 in the file App.java, as this is where the Strict Mode violation is being triggered. This is likely the code that is responsible for acquiring the resource that is being leaked.

  2. Look for any places in your code where you might be opening resources, such as streams or database connections, and make sure that you are properly closing them when you are finished with them.

  3. If you are unable to find the cause of the leak in your code, you could try enabling more verbose logging in Strict Mode by setting the penaltyLog flag in the VmPolicy.Builder object. This will cause Strict Mode to print a more detailed log message when a violation occurs, which might provide more clues about the cause of the leak.

  4. Another option might be to try running your app in a debugger and setting a breakpoint at line 30 in App.java. This will allow you to pause the app at the point where the leak is occurring, and inspect the state of the app to see if you can find any clues about the cause of the leak.
I hope this helps! Let me know if you have any other questions.
 
  • Like
Reactions: becauseWhyNot
Upvote 0
hi
To help you troubleshoot the issue you are experiencing, it might be helpful to try the following steps:

  1. Review the code around line 30 in the file App.java, as this is where the Strict Mode violation is being triggered. This is likely the code that is responsible for acquiring the resource that is being leaked.

  2. Look for any places in your code where you might be opening resources, such as streams or database connections, and make sure that you are properly closing them when you are finished with them.

  3. If you are unable to find the cause of the leak in your code, you could try enabling more verbose logging in Strict Mode by setting the penaltyLog flag in the VmPolicy.Builder object. This will cause Strict Mode to print a more detailed log message when a violation occurs, which might provide more clues about the cause of the leak.

  4. Another option might be to try running your app in a debugger and setting a breakpoint at line 30 in App.java. This will allow you to pause the app at the point where the leak is occurring, and inspect the state of the app to see if you can find any clues about the cause of the leak.
I hope this helps! Let me know if you have any other questions.

Thanks for the suggestions.
Line 30 is just the line that sets strict mode policy up so that I can capture the event. The issue still occur if I remove this:

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedClosableObjects()
.penaltyListener( this.getMainExecutor(), ( Violation v) -> {
v.fillInStackTrace();
v.printStackTrace();
} )

I will try the other suggestions to
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones