I'm putting together a codebase at the moment in which every (major) object inherits from a grand "Object" class. That base class keeps an internal reference count which callers can increment or decrement with obj->retain() and obj->release(). If the reference count ever drops to 0 the object deletes itself. As an extra connivence a user can call obj->autorlease() to add the object an an autorelease pool, which is drained periodically in the program's main run loop.
This is essentially the memory management architecture in Objective-C, and so far I'm really enjoying using it in a C++ context. What occurs to me, however, is that this pattern doesn't seem to be used terribly much in the C++ ecosystem; though I could be wrong about that, because I haven't seen all that much C++ code in the wild. Still, I get the feeling that smart pointers and other such things are strongly preferred in the C++ community over intrinsically reference counted objects.
I'm throwing this topic out there in the hopes of soliciting comments and sparking discussion. Why is it that the Objective-C style of memory management isn't used more pervasively in C++? (Or is it, and I'm just not aware).
This should be moved to lounge, but here is my view on it:
You are absolutely right, c++ as a language does not have a built in garbage collector like most languages do. Things like smart pointers and such were released in C++11 and even at that, not a lot of people know about them and a few make use of them. I feel like C++ is at the stage where it could be a complete language, with garbage collection, built in threading and synchronization abilities and all, but I feel like the task of transforming C++ into such a Java-like beast (not that it isn't already), is quite daunting and not for the faint of heart to take on. I dream of the day when I can code in C++ as I can in Java.
Why should the users of your system care about reference counting, what if they get it wrong? This is one of the problems with COM that was never really solved properly, even with ATL that manages client side stuff (including counts).
I can understand if you've inherited such a system, but why create a new one with this flaw?
This is essentially the memory management architecture in Objective-C...
ever herd of Automatic Reference Counting?
"Automatic Reference Counting (ARC) is a compiler feature that provides automatic memory management of Objective-C objects. Rather than having to think about retain and release operations, ARC allows you to concentrate on the interesting code, the object graphs, and the relationships between objects in your application."