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

Potential Memory Leak #5

Closed
kemasdimas opened this issue Mar 5, 2013 · 3 comments
Closed

Potential Memory Leak #5

kemasdimas opened this issue Mar 5, 2013 · 3 comments
Assignees

Comments

@kemasdimas
Copy link
Collaborator

Great library for easy form validation in Android, but I have a concern about potential memory leak in AbstractValidator class.

    protected Context mContext;

    public AbstractValidator(Context c) {
        mContext = c;
    }

I found out that this Context reference, if instantiated inside a Fragment that setRetainInstance == true, well retain a reference to the first activity passed in constructor.

AFAIK, according to Android Activity lifecycle, when there's configuration change, e.g when user change display orientation, an Activity will be destroyed and recreated. But a setRetainInstance == true fragment, will not be recreated on a configuration change.

So, in the end of Activity recreation, my retained fragment still hold a reference to the previously 'destroyed' Activity context, which is certainly a memory leak. CMIIW

I would like to contribute in this library, planning to have a pull request of my suggested implementation within this week :)

@throrin19
Copy link
Owner

Thank you for your help. Yes, I'm still struggling to find a good practice to avoid memory Leak. You can suggest any improvements for the project ^^

@ghost ghost assigned kemasdimas May 5, 2013
@throrin19 throrin19 reopened this Apr 30, 2014
@2bard
Copy link
Contributor

2bard commented Apr 30, 2014

Here we need to tell the Validator that a new Activity is running things now.

We can do this by overriding onActivityCreated in our fragment, it is called when an activity is created (even if setRetainInstance is set to true). In onActivityCreated we should update any validator with the new Context, this will remove the reference to our old Context and allow it to be garbage collected.

The current AbstractValidator doesn't allow us to update the Context. A work around for this would be to instantiate a new Validator each time a new Activity is created. I think it would be nicer to be able to provide a new Context to the AbstractValidator

@throrin19
Copy link
Owner

Thanks for all @2bard

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

No branches or pull requests

3 participants