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

Listeners are not cleaned up immediately when calling EventBus#unregister #39

Open
tterrag1098 opened this issue Jun 9, 2020 · 3 comments

Comments

@tterrag1098
Copy link
Collaborator

ListenerList#forceRebuild simply sets a boolean to true, and relies on the next event post to do the actual cleanup:

https://github.com/MinecraftForge/EventBus/blob/master/src/main/java/net/minecraftforge/eventbus/ListenerList.java#L223-L231

This means that if unregister is called and the event is never fired again (such as for startup events) the event listener will not be released from memory.

Additionally, there's no way to do this manually even though getListeners is public, since it requires the bus ID which is not exposed.

@cpw
Copy link
Contributor

cpw commented Jul 26, 2020

Doing otherwise can result in a race condition sadly.

@tterrag1098
Copy link
Collaborator Author

Could we at least expose a way to do it manually in situations where it will definitely be safe?

@ViRb3
Copy link

ViRb3 commented Nov 10, 2020

As a work around, you can use Java WeakReferences to avoid the listeners preventing large objects from being garbage collected. Check out the PR linked above for an example.

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