Skip to content

Prevent memory leak(s) from QuizChecker. #2656

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

Merged
merged 1 commit into from
Mar 19, 2019

Conversation

dbrant
Copy link
Collaborator

@dbrant dbrant commented Mar 19, 2019

Here's a general guideline for designing singleton classes:

  • Do not ever store instances of Context as a member variable in your class. Even if you're absolutely sure that you will clear out the Context when you're done, you will forget, and it will cause a memory leak because you will give it an instance of Activity by mistake.

Anyway, in this case it wasn't even necessary to keep an instance of Context. Also, this class maintains an instance of CompoundDisposable that keeps the results of Rx observables, which is great, but it never gets disposed! I hooked up the correct disposal mechanism.

@codecov-io
Copy link

Codecov Report

Merging #2656 into master will decrease coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #2656      +/-   ##
=========================================
- Coverage    2.75%   2.75%   -0.01%     
=========================================
  Files         260     260              
  Lines       12430   12432       +2     
  Branches     1125    1125              
=========================================
  Hits          343     343              
- Misses      12060   12062       +2     
  Partials       27      27
Impacted Files Coverage Δ
...ain/java/fr/free/nrw/commons/quiz/QuizChecker.java 0% <0%> (ø) ⬆️
...r/free/nrw/commons/contributions/MainActivity.java 0% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ceaaa9...faef49d. Read the comment docs.

@@ -37,7 +34,6 @@
private boolean isUploadCountFetched;

private CompositeDisposable compositeDisposable = new CompositeDisposable();
public Context context;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here application context is being injected by dagger. @dbrant Is having an instance of application context also considered dangerous?

In this particular class, I agree that storing the context isn't required and activity context can be used everywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here application context is being injected by dagger.

LeakCanary was showing a leak that pointed to the QuizChecker class, and when I looked at it, my eyes automatically jumped to the instance of Context.
But in fact the leak was caused by failing to dispose your CompoundDisposable, which this PR also fixes.

Technically, storing a reference to the application context isn't so bad. However, since Android (in its wisdom) doesn't differentiate between application and activity contexts, it's simply too dangerous to do this. There are almost always ways of accomplishing what you want without keeping a reference to a Context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for explaining. Will avoid storing a reference of the context as much as possible. :)

@misaochan
Copy link
Member

misaochan commented Mar 19, 2019

Whoops, yes, I remember this advice being mentioned at the 2017 hackathon, lol. Seems like we missed this issue when we code reviewed the PR.

I'll add a note to our wiki so hopefully it doesn't happen again.

@misaochan misaochan merged commit 8cd87ad into commons-app:master Mar 19, 2019
@dbrant dbrant deleted the memoryLeak2 branch March 19, 2019 13:38
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 this pull request may close these issues.

4 participants