Mercurial > lbo > hg > leveldb-rs
changeset 550:faa58d765e30
#13: fix memory leak in LRUList. The blockcache tests are now leak-free
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 06 Mar 2022 21:42:34 +0100 |
parents | 8cf7d6fba366 |
children | 05dac197bc8d |
files | src/cache.rs |
diffstat | 1 files changed, 9 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/src/cache.rs Sun Mar 06 20:13:10 2022 +0100 +++ b/src/cache.rs Sun Mar 06 21:42:34 2022 +0100 @@ -18,14 +18,15 @@ /// This is likely unstable; more investigation is needed into correct behavior! impl<T> LRUList<T> { fn new() -> LRUList<T> { - LRUList { + let l = LRUList { head: LRUNode { data: None, next: None, prev: None, }, count: 0, - } + }; + l } /// Inserts new element at front (least recently used element) @@ -90,21 +91,21 @@ fn remove(&mut self, node_handle: LRUHandle<T>) -> T { unsafe { + let d = replace(&mut (*node_handle).data, None).unwrap(); // If has next if let Some(ref mut nextp) = (*node_handle).next { swap(&mut (**nextp).prev, &mut (*node_handle).prev); } // If has prev if let Some(ref mut prevp) = (*node_handle).prev { - // swap prev.next - // (node_handle will own itself now) - swap(&mut (**prevp).next, &mut (*node_handle).next); + // swap prev.next with sink. sink will be dropped. + let mut sink = (*node_handle).next.take(); + swap(&mut (**prevp).next, &mut sink); } self.count -= 1; - // node_handle now only has references/objects that point to itself, - // so it's safe to drop - replace(&mut (*node_handle).data, None).unwrap() + + d } }