Closed Bug 1175355 Opened 9 years ago Closed 9 years ago

[Linter: HandlerLeak] Audit `new Handler()`s for resource leaks

Categories

(Firefox for Android Graveyard :: General, defect)

All
Android
defect
Not set
normal

Tracking

(firefox41 fixed)

RESOLVED FIXED
Firefox 41
Tracking Status
firefox41 --- fixed

People

(Reporter: mcomella, Assigned: mcomella)

References

Details

Attachments

(5 files)

HandlerLeak: Handler reference leaks
../../src/main/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/cellscanner/CellScanner.java:75: This Handler class should be static or leaks might occur (new android.os.Handler(){})

  72                 new IntentFilter(Reporter.ACTION_NEW_BUNDLE));
  73 
  74         // This is to ensure the broadcast happens from the same thread the CellScanner start() is on
  75         mBroadcastScannedHandler = new Handler() {
  76             @Override
  77             public void handleMessage(Message msg) {

../../src/main/java/org/mozilla/gecko/background/preferences/PreferenceFragment.java:56: This Handler class should be static or leaks might occur (new android.os.Handler(){})

  53   // The issue is that the Handler references the Fragment; messages targeting
  54   // the Handler reference it; and if such messages are long lived, the Fragment
  55   // cannot be GCed. This is not an issue for us; our messages are short-lived.
  56   private final Handler mHandler = new Handler() {
  57     @Override
  58     public void handleMessage(Message msg) {

../../src/main/java/org/mozilla/search/SearchActivity.java:111: This Handler class should be static or leaks might occur (new android.content.AsyncQueryHandler(){})

 108         // Initialize the fragments with the selected search engine.
 109         searchEngineManager.getEngine(this);
 110 
 111         queryHandler = new AsyncQueryHandler(getContentResolver()) {};
 112 
 113         searchBar = (SearchBar) findViewById(R.id.search_bar);

../../src/main/java/org/mozilla/gecko/toolbar/ToolbarProgressView.java:71: This Handler class should be static or leaks might occur (new android.os.Handler(){})

  68         mBounds = new Rect(0,0,0,0);
  69         mTargetProgress = 0;
  70 
  71         mHandler = new Handler() {
  72             @Override
  73             public void handleMessage(Message msg) {

Priority: 4 / 10
Category: Performance
Severity: Warning
Explanation: Handler reference leaks.
Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object. 

More info:

To suppress this error, use the issue id "HandlerLeak" as explained in the Suppressing Warnings and Errors section.
Assignee: nobody → michael.l.comella
Bug 1175355 - Add WeakReference into PreferenceFragment's Handler. r=nalexander
Attachment #8625986 - Flags: review?(nalexander)
Bug 1175355 - Add AsyncQueryHandlerImpl in SearchActivity. r=nalexander

This avoids the "HandlerLeak" Android Lint warning.
Attachment #8625987 - Flags: review?(nalexander)
Bug 1175355 - Add ToolbarProgressHandler into ToolbarProgressView. r=nalexander
Attachment #8625988 - Flags: review?(nalexander)
Bug 1175355 - Add BroadcastScanneredHandler to CellScanner. r=nalexander
Attachment #8625989 - Flags: review?(nalexander)
Attachment #8625987 - Flags: review?(nalexander)
Comment on attachment 8625987 [details]
MozReview Request: Bug 1175355 - Add AsyncQueryHandlerImpl in SearchActivity. r=nalexander

https://reviewboard.mozilla.org/r/11967/#review10575

::: mobile/android/search/java/org/mozilla/search/SearchActivity.java:74
(Diff revision 1)
> -    private AsyncQueryHandler queryHandler;
> +    private static AsyncQueryHandler queryHandler;

s/static//.
Comment on attachment 8625989 [details]
MozReview Request: Bug 1175355 - Add BroadcastScanneredHandler to CellScanner. r=nalexander

https://reviewboard.mozilla.org/r/11971/#review10577

Ship It!
Attachment #8625989 - Flags: review?(nalexander) → review+
Comment on attachment 8625985 [details]
MozReview Request: Bug 1175355 - Add WeakReferenceHandler. r=nalexander

https://reviewboard.mozilla.org/r/11963/#review10579

Ship It!
Attachment #8625985 - Flags: review?(nalexander) → review+
Comment on attachment 8625986 [details]
MozReview Request: Bug 1175355 - Add WeakReference into PreferenceFragment's Handler. r=nalexander

https://reviewboard.mozilla.org/r/11965/#review10581

Ship It!
Attachment #8625986 - Flags: review?(nalexander) → review+
Comment on attachment 8625987 [details]
MozReview Request: Bug 1175355 - Add AsyncQueryHandlerImpl in SearchActivity. r=nalexander

https://reviewboard.mozilla.org/r/11967/#review10583

Ship It!
Attachment #8625987 - Flags: review+
Comment on attachment 8625988 [details]
MozReview Request: Bug 1175355 - Add ToolbarProgressHandler into ToolbarProgressView. r=nalexander

https://reviewboard.mozilla.org/r/11969/#review10585

Ship It!
Attachment #8625988 - Flags: review?(nalexander) → review+
Product: Firefox for Android → Firefox for Android Graveyard
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: