Tom Ritchford
1 min readApr 15, 2024

--

Honestly, in my experience 90% of the uses of std::shared_ptr are wrong.

Using a shared pointer says that you have no idea when or where your pointer is going to be deleted: it might even be in a different thread than the one that created the pointer!

But there really aren't many good reasons that you don't know this. The only time I had to use this in my own code was when writing a Python module, when one or more Python objects kept a pointer to a shared data structure, and then of course C++ has no idea when Python is going to lose the last reference.

I also worked on a complicated shared ledger system which had been designed with shared pointers, and again, there was no way to predict when the last reference would vanish.

But a lot of the time, people using shared pointers simply want the functionality of Java, where you can copy pointers to objects around freely and not care about the garbage collection.

--

--