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

Wrap Listener instances in WeakRef #477

Merged
merged 2 commits into from Aug 23, 2020

Conversation

jonathanhefner
Copy link
Contributor

@jonathanhefner jonathanhefner commented Jul 5, 2020

The @listeners variable holds a reference to each Listener instance created by Listen.to and can only be cleared by calling Listen.stop. This can prevent Listener instances from being garbage collected if they are abandoned before Listen.stop is called.

This commit wraps Listener instances in WeakRef before adding them to @listeners, to allow them to be garbage collected.

Partially addresses #476.

@coveralls
Copy link

coveralls commented Jul 5, 2020

Coverage Status

Coverage increased (+0.1%) to 97.831% when pulling 63eff89 on jonathanhefner:listeners-memory-leak into ba5059c on guard:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 97.757% when pulling 694e28b on jonathanhefner:listeners-memory-leak into ba5059c on guard:master.

@jonathanhefner jonathanhefner changed the title Cease holding references to listeners Wrap Listener instances in WeakRef Jul 21, 2020
@jonathanhefner
Copy link
Contributor Author

jonathanhefner commented Jul 21, 2020

@headius Does this look all right to you? I decided to forgo clearing dead WeakRefs in Listen.to because it would require using Mutex instead of a simple Queue to stay thread-safe. I can make that change if you prefer. However, if someone were to create so many listeners that dead WeakRefs could become a problem, they might prefer using Listener.new instead (since it will have less overhead).

The `@listeners` variable holds a reference to each `Listener` instance
created by `Listen.to` and can only be cleared by calling `Listen.stop`.
This can prevent `Listener` instances from being garbage collected if
they are abandoned before `Listen.stop` is called.

This commit wraps `Listener` instances in `WeakRef` before adding them
to `@listeners`, to allow them to be garbage collected.

Partially addresses guard#476.
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.

None yet

3 participants