Object Ownership -- Ilya Doroshenko
This article goes over the spicy topic of object ownership. We covered the lifetime quirks, and we found out that manual memory management can be a nightmare, we new and delete in the correct order. There must be something better than that. Well, there is but it comes with its own can of worms.
Object Ownership
By Ilya Doroshenko
From the article:
Since we know the rules of
newanddelete, namelynewallocates anddeletedestroys, we never really cared about who is responsible for the object. This caused a lot of confusion in the past. For instance, some API codes from Win32 return strings that should beLocalFree()d, likeFormatMessageorGetEnvironmentStrings. POSIX, on the other hand, hasstrdupas a common example of you should free it yourself. This model is confusing because you may have a lot of return statements, before which you should always callfreeordelete, depending on the operation. However, we have RAII since the very beginning of C++, which adds constructors and destructors. So, in 1998 resourceful people decided to addauto_ptrto the standard.The premise was simple:
- a simple explicit constructor, that took raw pointer from
new- destroyed by either an explicit
release/resetor a destructor on the end of the blockThis was the first attempt at a structured cleanup. As time passed, the initial point began to crumble. More issues arose and the question came up: Who owns the data?


Visual Studio 17.6 comes with new functionality in the Address Sanitizer runtime which provides a new “checked build” for C and C++. This new runtime mode diagnoses and reports hidden memory safety errors, with zero false positives, as your app runs.
The Monitor Object design pattern synchronizes concurrent member function execution to ensure that only one member function runs within an object at a time. It also allows an object's member functions to schedule their execution sequences cooperatively.
The latest major version of the
The active object design pattern decouples method execution from method invocation for objects that each reside in their own thread of control.The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.
A few years ago, I showed an interesting implementation for self-registering classes in factories. It works, but one step might be at the edge of Undefined behavior. Fortunately, with C++20, its new constinit keyword, we can update the code and ensure it’s super safe.
You may have a class that you want to participate in RVO or NRVO, but you also don’t want it to be moved. For example, it may contain a std::mutex, which is not movable. But you nevertheless have to declare a move constructor. What can you do?
Sorting algorithms have been thoroughly studied. Kevlin Henney takes an unexpected paradigm journey into sleep sort.