Open gftea opened 2 years ago
Notice the comment that says, "Wouldn't it be nice if heap::allocate
worked like this?" It's not a real function
If heap::deallocate is to free heap, why need to calling ptr::read which will also drop the memory
@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.
@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
@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.
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
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.
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
That's mostly correct. ptr::read
is used to call the drop
method of the value, if it implements Drop
after re-reading https://doc.rust-lang.org/nomicon/destructors.html, I think it will be more clear to explain further below
self.ptr
is raw pointer, so we call ptr::read(self.ptr)
to drop the field explicitly
A example in https://github.com/rust-lang/nomicon/blob/master/src/leaking.md
heap::deallocate
in rust std library.heap::deallocate
is to free heap, why need to callingptr::read
which will also drop the memory