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

Box is missing leak_with_allocator #134

Open
matthewpipie opened this issue Sep 6, 2024 · 2 comments
Open

Box is missing leak_with_allocator #134

matthewpipie opened this issue Sep 6, 2024 · 2 comments

Comments

@matthewpipie
Copy link

Box::leak is defined as

unsafe { &mut *Box::into_raw(b) }

However, there is no way to extract the allocator while leaking without using the underlying Box::into_raw_with_allocator, which returns (*mut T, A). I propose adding Box::leak_with_allocator, which returns (&mut T, A) by calling into_raw_with_allocator and performing the unsafe cast.

Any thoughts on this? Happy to make a PR if this is something that others think is a good idea.

@Amanieu
Copy link
Member

Amanieu commented Nov 7, 2024

Sorry for the late response. Yes! A PR would be appreciated.

@zakarumych
Copy link

zakarumych commented Nov 7, 2024

This call wouldn't be safe.
Dropping allocator may effectively free the memory.
And reference would be still around.

With Global it is known that dropping allocator doesn't invalidate the memory. So Box::leak is safe.

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