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

AndroidLeakFixes.FLUSH_HANDLER_THREADS cause IdleHandler runs every second #2691

Open
NamekMaster opened this issue Jun 28, 2024 · 1 comment

Comments

@NamekMaster
Copy link

Description

I implemented a IdleHandler to do someting for my HandlerThread, but I see the IdleHandler runs every second. According to the code in AndroidLeakFixes.FLUSH_HANDLER_THREADS, it adds an IdleHandler and postDelayed({}, 1000), so that cause my IdleHandler to be executed per second, is it a potential bug? If it is an intended behavior, how to stop it from affecting my logic ? I don't want to execute my IdleHandler every second.

@square square deleted a comment Sep 17, 2024
@pyricau
Copy link
Member

pyricau commented Sep 17, 2024

See the doc here:

/**
* HandlerThread instances keep local reference to their last handled message after recycling it.
* That message is obtained by a dialog which sets on an OnClickListener on it and then never
* recycles it, expecting it to be garbage collected but it ends up being held by the
* HandlerThread.
*/
FLUSH_HANDLER_THREADS {

The core issue is that any idle Handler thread will keep a strong reference to the last message that ran, but that message will also be recycled, then accidentally used by a dialog to store some OnClickListener in its callback field, and that field will not be cleared after the dialog is destroyed, so the idle handler will end up keeping a strong reference to on click listeners of destroyed dialogs.

I haven't found any better fix than making sure HandlerThreads aren't ever idle for too long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants