Finding what violated StrictMode policy
Asked Answered
H

4

17

I've enabled StrictMode in my app and it's causing a few crashes as expected. How can I find out where in my code I'm violating these policies?

This is the stack trace:

E/AndroidRuntime(19523): FATAL EXCEPTION: main
E/AndroidRuntime(19523): android.os.StrictMode$StrictModeViolation: policy=95 violation=2
E/AndroidRuntime(19523):        at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1326)
E/AndroidRuntime(19523):        at android.os.StrictMode.access$1300(StrictMode.java:111)
E/AndroidRuntime(19523):        at android.os.StrictMode$AndroidBlockGuardPolicy.handleViolation(StrictMode.java:1319)
E/AndroidRuntime(19523):        at android.os.StrictMode$AndroidBlockGuardPolicy$1.run(StrictMode.java:1206)
E/AndroidRuntime(19523):        at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(19523):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(19523):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(19523):        at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(19523):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19523):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(19523):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
E/AndroidRuntime(19523):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
E/AndroidRuntime(19523):        at dalvik.system.NativeStart.main(Native Method)

but as you can see ... it's not very useful ... I know who killed my app, I need to know why!

Thanks.

Hyperbaric answered 20/3, 2012 at 3:19 Comment(2)
How did you enable SrictMode? Please paste the code so we can inspect it.Hyehyena
DetectAll.penaltyLog().penaltyDeath() for both. Looking at your response below, it just looks like I need to add "StrictMode" to my log filters :)Hyperbaric
H
24

You need to call penaltyLog() on your StrictMode.ThreadPolicy.Builder, so that it will show you the underlying reason as well as stopping your app.

Here's what you probably have currently:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyDeath()
.build());

If you call the network on the main thread, you'll get this exception which is hard to understand:

E/AndroidRuntime(8752): android.os.StrictMode$StrictModeViolation: policy=71 violation=4
E/AndroidRuntime(8752):     at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1311)

If you then add penaltyLog() to your policy...

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeath()
.build());

then you will see a much more helpful message like the one below. This will be in the LogCat output.

D/StrictMode(8810): StrictMode policy violation; ~duration=2956 ms: android.os.StrictMode$StrictModeNetworkViolation: policy=87 violation=4
D/StrictMode(8810):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1090)

If you look closely, you'll see that this stack trace will lead you to the code which is causing the StrictMode violation.

Hyehyena answered 20/3, 2012 at 4:26 Comment(3)
Note: This IS what I'm doing, but your log entry showed me that I need to adjust my log filters (which is what I was suspecting while trying to fall asleep :) ). You win the cake :) ThanksHyperbaric
Well, it doesn't look more helpful to me. How do I know what code it crashes on?Garver
How closely should I look? Even after using the microscope, I don't see any helpful message yet.Intermission
S
2

Whenever I see stack traces like this I always look into my Activity lifecycle events. Checkout what's going on in your onCreate, onResume, onPause methods (there are more lifecycle events but these are the common ones). Put break points in those methods and see which one terminates with this fatal message. Then take it from there.

Try catching this error using

protected void onResume() {
  super.onResume();
  try {
    codeThatCrashesBecauseOfStrictMode();
  } catch(Throwable tr) { Log.e(tr); }
}

This should be a pretty good starting point for debugging this problem.

Salman answered 20/3, 2012 at 4:24 Comment(0)
I
2

StrictMode (android.os.StrictMode) class can be used to enable and enforce various policies that can be checked for and reported upon.

This can be a StrictMode violation in executing a disk write violation that occurs when you are performing disk writes on the main UI thread. To solve it, you need to move the disk writes off the main thread.

If you can't move the code at this point, you could disable the check for a part of the code.

Explicitly add code to stop checking for a particular rule violation just before the offending code is executed and then re-enable detection for that rule after the offending code has completed.

StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
    .permitDiskWrites()
    .build());
doCorrectStuffThatWritesToDisk();
StrictMode.setThreadPolicy(old);

Source code taken from here.

Inclement answered 10/1, 2015 at 10:48 Comment(1)
I think you should reference the answer you took that code from: https://mcmap.net/q/506319/-strictmodediskreadviolation-whenConeflower
S
-1

even though this does nothing to understand root cause, would it help you to simply add "strict=False" to your Library definition? (note that my context is Robot Framework *** Settings *** where I include "Browser" library): eg:

Library     Browser     strict=False
Sternberg answered 31/1, 2022 at 1:7 Comment(1)
Making it not crash without actually fixing the root cause defeats the purpose.Hyperbaric

© 2022 - 2024 — McMap. All rights reserved.