Posts

Showing posts from February, 2023

Contexted drop in Rust

 In game development, it is often the case that, for performance reasons we allocate objects from a pool. When we want to clean up an allocated object, we need to release it back to the pool it was allocated from. Of course C++ is an unsafe language . It is very easy to cause one of various errors with such a pattern: Forget to free an allocated object (memory leak) The pool is freed before the allocated objects (dangling pointers) The pool is accessed unsafely from multiple threads (data races) We free the object to another pool than the one it was allocated from  Pool allocations have another quirk. They feel non-idiomatic . The idiomatic way of freeing an object in C++ is relying on its destructor. However, passing a pool as an argument to destructors in C++ is not possible as destructors in C++ take no arguments. The only workaround seems to be storing a pointer or reference to the pool in each allocated object. While this will allow easy cleanup of allocated objects through their