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

what is heap::deallocate? #370

Open
gftea opened this issue Jun 25, 2022 · 10 comments
Open

what is heap::deallocate? #370

gftea opened this issue Jun 25, 2022 · 10 comments

Comments

@gftea
Copy link
Contributor

gftea commented Jun 25, 2022

A example in https://github.com/rust-lang/nomicon/blob/master/src/leaking.md

  • I cannot find heap::deallocate in rust std library.
  • If heap::deallocate is to free heap, why need to calling ptr::read which will also drop the memory
// drop the data and then free it
                ptr::read(self.ptr);
                heap::deallocate(self.ptr);
@botahamec
Copy link

Notice the comment that says, "Wouldn't it be nice if heap::allocate worked like this?" It's not a real function

@gftea
Copy link
Contributor Author

gftea commented Jul 1, 2022

If heap::deallocate is to free heap, why need to calling ptr::read which will also drop the memory

@botahamec
Copy link

botahamec commented Jul 2, 2022

@gftea Not every type implements Drop, and the types that do won't know that they've been allocated on the heap, or where they are in memory, so they can't free themselves. For example, String contains a pointer to some memory that's on the heap. When it's dropped, it'll deallocate the memory that it put on the heap, but it can't deallocate itself, so if you don't deallocate it, the pointer to free memory will remain on the heap, unused.

@gftea
Copy link
Contributor Author

gftea commented Jul 3, 2022

@botahamec , thanks, but in our example, why ptr::read needed?, it just "Reads the value from src without moving it. This leaves the memory in src unchanged."

                ptr::read(self.ptr);              // this read value of RcBox, leave self.ptr unchanged, what is point here?
                heap::deallocate(self.ptr); // free the heap for RcBox  poninted by self.ptr

@botahamec
Copy link

@gftea According to the comment, it is being used to drop the value, if needed. Just keep in mind that dropping the value doesn't free the memory that it's in.

@gftea
Copy link
Contributor Author

gftea commented Jul 3, 2022

After the value drop, it should be uninitialized, we should not access it after that, so I think we should deallocate first before dropping ptr

@botahamec
Copy link

If you deallocate first, then how are you going to access the value so you can drop it? You don't have permission to use that part of the memory anymore. Besides, you don't have to access the value to deallocate memory. All you need is the pointer to where it was.

@gftea
Copy link
Contributor Author

gftea commented Jul 3, 2022

it is being used to drop the value, if needed. Just keep in mind that dropping the value doesn't free the memory that it's in.

sorry, I guess I did not understand above answer.
Now if I understand it correctly, calling ptr::read(self.ptr) is to make sure trigger drop of all nested fields in value pointed by self.ptr , then use deallocate to free the memory pointed by self.ptr

@botahamec
Copy link

That's mostly correct. ptr::read is used to call the drop method of the value, if it implements Drop

@gftea
Copy link
Contributor Author

gftea commented Jul 6, 2022

after re-reading https://doc.rust-lang.org/nomicon/destructors.html, I think it will be more clear to explain further below

  • for non-raw pointer field, rust will call drop automatically
  • because self.ptr is raw pointer, so we call ptr::read(self.ptr) to drop the field explicitly

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

2 participants