Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrictMode disk access violation #15

Closed
danh32 opened this issue May 9, 2015 · 4 comments · Fixed by #28
Closed

StrictMode disk access violation #15

danh32 opened this issue May 9, 2015 · 4 comments · Fixed by #28

Comments

@danh32
Copy link

danh32 commented May 9, 2015

With StrictMode fully enabled:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .penaltyDeath()
        .build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .penaltyDeath()
        .build());

I'm getting a crash on launch when calling LeakCanary.install(this);

StrictMode  D  StrictMode policy violation; ~duration=8 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=95 violation=2
            D      at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1137)
            D      at libcore.io.BlockGuardOs.access(BlockGuardOs.java:67)
            D      at java.io.File.doAccess(File.java:283)
            D      at java.io.File.exists(File.java:363)
            D      at android.app.ContextImpl.createFilesDirLocked(ContextImpl.java:997)
            D      at android.app.ContextImpl.getFilesDir(ContextImpl.java:1020)
            D      at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:201)
            D      at com.squareup.leakcanary.AndroidHeapDumper.<init>(AndroidHeapDumper.java:29)
            D      at com.squareup.leakcanary.LeakCanary.androidWatcher(LeakCanary.java:68)
            D      at com.squareup.leakcanary.LeakCanary.install(LeakCanary.java:58)
            D      at com.squareup.leakcanary.LeakCanary.install(LeakCanary.java:43)
            D      at com.robinhood.android.App.onCreate(App.java:56)
            D      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1011)
            D      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4518)
            D      at android.app.ActivityThread.access$1500(ActivityThread.java:144)
            D      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)
            D      at android.os.Handler.dispatchMessage(Handler.java:102)
            D      at android.os.Looper.loop(Looper.java:135)
            D      at android.app.ActivityThread.main(ActivityThread.java:5221)
            D      at java.lang.reflect.Method.invoke(Native Method)
            D      at java.lang.reflect.Method.invoke(Method.java:372)
            D      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            D      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
AndroidRuntime  D  Shutting down VM

Calling LeakCanary.install() on a background thread fixes the issue, just thought it may be worth addressing in the lib (or in the readme) since others may also have StrictMode fully enabled for debug builds.

@pyricau
Copy link
Member

pyricau commented May 10, 2015

That's really interesting. I thought of Context.getFilesDir() as a no-op, didn't think it's do some actual file reading.

We can work around that, delay the call to Context.getFilesDir() by keeping a ref to the application context and doing it at heap dump time (which happens in a background thread)

@pyricau
Copy link
Member

pyricau commented May 10, 2015

There's more to it than just that, we'll need to update http://github.com/square/haha as well.

pyricau added a commit that referenced this issue May 10, 2015
* Heap dumps and analysis results were previously saved in the app directory. They are now saved on the external storage (sd card).
* Centralized internal static helper methods to a dedicated class: `LeakCanaryInternals`.

BREAKING CHANGES

* When upgrading, previously saved heap dumps will be lost, but won't be removed from the app directory. You should probably uninstall your app.
* Added permission WRITE_EXTERNAL_STORAGE
* Public API change: Removed `Application` parameter in `LeakCanary.androidWatcher()`
* Public API change: Removed `Application` parameter in `AndroidHeapDumper()`

* Fixes #21 (can't share heap dump)
* This is a step towards fixing #15 (strict mode violations), although there's still more work.
This was referenced May 10, 2015
@pyricau
Copy link
Member

pyricau commented May 12, 2015

As we found in #61, LeakCanaryInternals#setEnabled should be in background.

pyricau added a commit that referenced this issue May 12, 2015
* Enable Strict Mode thread policy in the sample app.
* Fixed violation when cleaning up files on startup.
* Fixed violation when enabling components, the other side of the binder does IO operations.

Fixes #15
pyricau added a commit that referenced this issue May 12, 2015
Fix Strict mode violations. Fixes #15.
@Jacksgong
Copy link

Well done!

Yky pushed a commit to Yky/leakcanary that referenced this issue Feb 21, 2016
* Heap dumps and analysis results were previously saved in the app directory. They are now saved on the external storage (sd card).
* Centralized internal static helper methods to a dedicated class: `LeakCanaryInternals`.

BREAKING CHANGES

* When upgrading, previously saved heap dumps will be lost, but won't be removed from the app directory. You should probably uninstall your app.
* Added permission WRITE_EXTERNAL_STORAGE
* Public API change: Removed `Application` parameter in `LeakCanary.androidWatcher()`
* Public API change: Removed `Application` parameter in `AndroidHeapDumper()`

* Fixes square#21 (can't share heap dump)
* This is a step towards fixing square#15 (strict mode violations), although there's still more work.
Yky pushed a commit to Yky/leakcanary that referenced this issue Feb 21, 2016
* Enable Strict Mode thread policy in the sample app.
* Fixed violation when cleaning up files on startup.
* Fixed violation when enabling components, the other side of the binder does IO operations.

Fixes square#15
Yky pushed a commit to Yky/leakcanary that referenced this issue Feb 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants