-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Box::leak will not call allocator destruction function #106207
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
Comments
x is never dropped though - it is leaked.
The Box is wrapped in ManuallyDrop. This does not mean that the value is dropped! Instead, it means that the value will not be dropped automatically by the compiler. The destructor not running seems like the intended behaviour to me. |
Hm, seems a bit inconsistent if we compare to the |
https://doc.rust-lang.org/src/alloc/boxed.rs.html#197-200 Yeah, obviously the destructor of value ( |
Sorry for using the wrong phrase |
I think this failing to drop the allocator is pretty surprising and is inconsistent with most uses of Really, it's hard for me to see a case where this is the desired behavior... |
While https://doc.rust-lang.org/stable/src/alloc/vec/mod.rs.html#818 |
In case of a multi-tiered allocator approach, where the last tier is something super simple, e.g. a bump allocator, I could see a use case where a child allocator returns the memory owned by itself to a parent allocator when its last instance is dropped. This obviously would be unsound if allocator instances can be dropped when leaking memory. The Safety comment of the allocator trait says (emphasis mine):
The safety comment could of course be adjusted to put the onus on the implementor to check for any leaks when the alst instance is dropped, but that prevents very simple allocator designs, since some book-keeping is needed. |
So, I think I was completely mistaken here. Or rather, I think it is surprising behavior, and I think it will usually not be desired, but it's also probably the correct behavior if we're going to offer this function for My guess is that there are going to be plenty of |
Also applies to `Vec::into_raw_parts`. The expectation is that you can round-trip these methods with `from_raw`, but this is only true when using the global allocator. With custom allocators you should instead be using `into_raw_with_alloc` and `from_raw_in`. Additionally, this PR fixes unsoundness in `Box::leak` when used with a custom allocator: previously the allocator contained in the `Box` was dropped. This is incorrect because for `leak` to be safe, the allocator must not free its underlying backing store. The `Allocator` trait only guarantees that allocated memory remains valid until the allocator is dropped. Fixes rust-lang#106207
The correct behavior is actually not to leak the allocator, otherwise #141219 moves Since this is behaving as intended, I'm going to close this issue. |
I tried this code:
I expected to see this happen:
x
get dropped, the ref count decreased to 1Instead, this happened:
x
get dropped, the ref count dose not changeThe current implementation of
Box::leak
willManuallyDrop
the wholeBox
but only leak the data pointer. The destruction function of the allocator will not be called. I'm not sure if it the expected behavior.Meta
rustc --version --verbose
:Backtrace
related to : #106203
The text was updated successfully, but these errors were encountered: