Concurrent access atomicptr

I have an AtomicPtr reference that gets updated and read concurrently. What is the typical paradigm for managing it? I have set it up the following way:

let new = Box::into_raw(...);
let result = aptr.compare_exchange(old, new, ...);
match result {
     Ok(old) => {
            // add old to some epoch based garbage collector.
     }
     Err(_) => {
            unsafe {new.drop_in_place()};
     }
}

Unfortunately the Box creation seems to leak a helluva lot of memory. Any ideas what I might be doing wrong?

drop_in_place runs the destructor for the value inside the Box, it doesn't run Box's destructor. It can't because the pointer doesn't know anything about where it came from.

You should use Box::from_raw to drop the Box itself.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.