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

Approaches to get a destructor when main finishes. #62

Closed
dpc opened this issue Mar 7, 2017 · 5 comments
Closed

Approaches to get a destructor when main finishes. #62

dpc opened this issue Mar 7, 2017 · 5 comments

Comments

@dpc
Copy link

dpc commented Mar 7, 2017

Hi, I have a problem that I wish I could rely somehow on destruction of lazy_static to call drop on what's inside it. I know it might be impossible, but I guess it's worth asking. Suggestions for workaround etc. are welcome. I was eg. thinking about using a Weak inside lazy_static, but then I do have to have a strong reference somewhere in the main thread.

@Kimundi
Copy link
Contributor

Kimundi commented Mar 7, 2017

Rust as a language has basically made the design decision to not have custom user code running before or after main(), so if a static variable contains a type with a destructor, that destructor will just never be called. As far as I'm aware, there is no good way to have a automatic destruction anyway, so lazy_static just mirrors that semantic.

However, the crate could provide something like lazy_static::destroy() for manually destroying a static. The issue there is just that the internally used std::Once has no way to be "reset", so someone who is more knowledgeable about thread synchronisation primitives than I would have to write a custom implementation for that (which would also be useful for #60).

@Kimundi
Copy link
Contributor

Kimundi commented Mar 7, 2017

One possible workaround you could do is to have the lazy_static be of a Mutex<Option<T>> or similar, and then destroy the T by overwriting the Option with a None

@dpc
Copy link
Author

dpc commented Mar 7, 2017

For synchronization I'm quite sure to resort to ArcCell from crossbeam or what you have mentioned. The API compilcations are main concern.

@KodrAus
Copy link
Contributor

KodrAus commented Sep 27, 2018

It's been a while now, so I'll go ahead and close this issue as a design decision we inherit from Rust upstream. Please feel free to re-open though if there's anything more you'd like to add!

@jtmoon79
Copy link

FYI this affects crate tempfile.

Stebalien pushed a commit to Stebalien/tempfile that referenced this issue Jul 15, 2022
documentation notes static declared instances may leak (like with lazy_static). Links to underlying reason
rust-lang-nursery/lazy-static.rs#62

Issue #183
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