fitzgen / bumpalo

A fast bump allocation arena for Rust
https://docs.rs/bumpalo
Apache License 2.0
1.41k stars 111 forks source link

Memory leak may happen after calling `Bump::dealloc`. #222

Closed RinChanNOWWW closed 11 months ago

RinChanNOWWW commented 11 months ago
impl Bump {
    #[inline]
    unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
        // If the pointer is the last allocation we made, we can reuse the bytes,
        // otherwise they are simply leaked -- at least until somebody calls reset().
        if self.is_last_allocation(ptr) {
            let ptr = NonNull::new_unchecked(ptr.as_ptr().add(layout.size()));
            self.current_chunk_footer.get().as_ref().ptr.set(ptr);
        }
    }
}

Setting self.current_chunk_footer directly will lose the control of the memory in ptr..ptr.add(layout.size()).

When calling dealloc_chunk_list in drop, we cannot find the memory between ptr..ptr.add(layout.size()).

fitzgen commented 11 months ago

I don't believe this is a leak:

RinChanNOWWW commented 11 months ago

Thank you. I misunderstood the implementation.