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

Weak values keep whole dictionary alive? #9028

Closed
eschnett opened this issue Nov 16, 2014 · 4 comments
Closed

Weak values keep whole dictionary alive? #9028

eschnett opened this issue Nov 16, 2014 · 4 comments

Comments

@eschnett
Copy link
Contributor

While browsing dict.jl, I've come across this function:

function add_weak_value(t::Dict, k, v)
    t[k] = WeakRef(v)
    finalizer(v, x->weak_value_delete!(t, k, x))
    return t
end

This looks as if the lambda passed to the finalizer keeps the whole dictionary (t) alive. This sounds dangerous if dictionaries with weak keys are transient (which may not be the "usual" way they are used).

@vtjnash
Copy link
Sponsor Member

vtjnash commented Nov 16, 2014

the lambda will be destroyed after the finalizer is run, allowing the dictionary to be freed. in what situation do you foresee this being dangerous?

@eschnett
Copy link
Contributor Author

I am thinking of a case where the dictionary can be garbage collected, but
one of the weak values is still alive (i.e. reachable independent of the
dictionary). In this case, it will hold the whole dictionary hostage until
the value itself becomes unreachable.

-erik

On Sunday, November 16, 2014, Jameson Nash notifications@github.com wrote:

the lambda will be destroyed after the finalizer is run, allowing the
dictionary to be freed. in what situation do you foresee this being
dangerous?

Reply to this email directly or view it on GitHub.<
https://ci6.googleusercontent.com/proxy/asLPJsV9RzNJy6Dq9KVx5dUa3I55wgunbWipR71qUVjqcPkD9zOs7j0OSu94t44y33SxXSukZXKi9FRDt7sbxp7YTjbi9OuJE2ZCA-Aydz3zh4c0Ey-gH0wEPloLlZ7v24813mgD_4GZlMtyn84_XPpp5SuCsg=s0-d-e1-ft#https://github.com/notifications/beacon/AANCCqHbUCXGXahLgoVGku2Luk-ynZx7ks5nOMP0gaJpZM4C76e_.gif

Erik Schnetter schnetter@gmail.com
http://www.perimeterinstitute.ca/personal/eschnetter/

@JeffBezanson
Copy link
Sponsor Member

Maybe we should go with the plan described by the TODO in add_weak_key, and remove weak keys/values lazily instead of with finalizers. That might be faster too, since it avoids the overhead of adding and running finalizers.

We could also consider adding this behavior to Dict instead of using a separate type.

Also, add_weak_value doesn't seem to be used anywhere.

@oxinabox
Copy link
Contributor

I think this can be closed as the problematic method add_weak_value no longer exists in master.

@vtjnash vtjnash closed this as completed May 30, 2019
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

4 participants