Giter Club home page Giter Club logo

Comments (12)

openproject avatar openproject commented on August 28, 2024 2

@gengjiawen
android.widget.Toast$TN.mNextView leak solution is
using
Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
instead of
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

from androidmvp.

antoniolg avatar antoniolg commented on August 28, 2024 1

I understand the point behind this issue, because it could lead to problems that the view keeps attached after the activity has been destroyed, but I don't see how to reproduce the leak you're mentioning.

The MainActivity will actually leak memory when I go back to LoginActivity while the presenter of the MainActivity is fetching data in background

This can't happen afaik, because you can't go back from MainActivity to LoginActivity, it will close the App. The activity will probably be leaked while the background task is being done, but it'll be released afterwards.

I track the memory leak using leakcanary, and MainActivity do leaked.

I added LeakCanary to the project, and was navigating for a while, but couldn't find a way to leak it. The message @gengjiawen is telling that is being leaked by a toast. I'm not very used to LeakCanary, so could be a reason why I don't find it. If you could please create a PR with LeakCanary included and a way to reproduce it, it'd be helpful.

Apart from that, it's true that the example is not perfect. The listener shouldn't be called if the activity is destroyed, or at least the activity shouldn't be updated. But background tasks shouldn't use handlers either, this example is just a simplification. Anyway, I'll look for the simplest possible way to fix this issue.

from androidmvp.

kaedea avatar kaedea commented on August 28, 2024

I have detect this by calling GC manually. The MainActivity will actually leak memory when I go back to LoginActivity while the presenter of the MainActivity is fetching data in background.
In my case, I will remove reference of IView in the Presenter when the Activity is onDestroy.

from androidmvp.

gengjiawen avatar gengjiawen commented on August 28, 2024

I track the memory leak using leakcanary, and MainActivity do leaked.

* com.antonioleiva.mvpexample.app.main.MainActivity has leaked:
* GC ROOT android.widget.Toast$TN.mNextView
* references android.widget.LinearLayout.mContext
* leaks com.antonioleiva.mvpexample.app.main.MainActivity instance

from androidmvp.

antoniolg avatar antoniolg commented on August 28, 2024

Thanks! Feel free to PR a solution, otherwise I'll try to find some time to fix this at some point.

from androidmvp.

raghunandankavi2010 avatar raghunandankavi2010 commented on August 28, 2024

@kaedea so you would do loginview = null when activity is destroyed?. Or may be using a weakreference might help??

from androidmvp.

raghunandankavi2010 avatar raghunandankavi2010 commented on August 28, 2024

@kaedea i did take a look at your implementation of mvp @ https://github.com/kaedea/android-mvp-pattern/blob/master/app/src/main/java/me/kaede/mvp/loginoptimized/presenter/LoginPresenterCompl.java where in you have iLoginView = null .

from androidmvp.

raghunandankavi2010 avatar raghunandankavi2010 commented on August 28, 2024

@antoniolg you are right. Thanks for the update.

from androidmvp.

gengjiawen avatar gengjiawen commented on August 28, 2024

@antoniolg In MainActivity just rotate the screen it will reproduce the bug if I remember correctly.
From the experience I deal with memory leak, "leaked by a toast" is mostly indicated leak an activity instance, which is very common.
I will test you fix on this issue when I got time :)

from androidmvp.

gengjiawen avatar gengjiawen commented on August 28, 2024

@openproject you are wrong, this is not the case.You leak a context, it will show leak log like that, it's has nothing to do with the Toast.
@antoniolg I tested on new code,the leak no longer exists:)

from androidmvp.

antoniolg avatar antoniolg commented on August 28, 2024

Thanks for testing it @gengjiawen !!

from androidmvp.

HarisA92 avatar HarisA92 commented on August 28, 2024

can someone post a solution for this problem? Thanks

from androidmvp.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.