DEV Community

Discussion on: How a weak_ptr might prevent full memory cleanup of managed object

Collapse
 
timidger profile image
Preston Carpenter

Ooo, you made me check and Rust has the same memory model for their Rc/Arc as C++'s shared_ptr. This is something I'll have to remember when using either language.

Collapse
 
fenbf profile image
Bartlomiej Filipek

I've just started with Rust... so you're saying that you can end up in a same situation as I described in C++?

Collapse
 
timidger profile image
Preston Carpenter

Yep! If you look at the Rust Arc/Rc source here you'll see they have the same memory representation.

However, the fix is a little bit less errorprone to write. You simply need to do Rc<Box<WhateverStruct>>

Thread Thread
 
fenbf profile image
Bartlomiej Filipek

ech... and people say that C++ is a complex language :)
Rust also looks scary :)
Thanks for sharing

Thread Thread
 
timidger profile image
Preston Carpenter

Ha that's a direct link to the source (and that was just to show you the memory layout).

Here's a much more user-friendly view into the non-thread-safe version of shared_ptr (the thread safe version is called Arc):