by jeremysalwen · 6y ago · 0 comments
Suppose you have a class
class Foo {
shared_ptr<Widget> widget;
bool has_widget();
void set_widget(shared_ptr<widget> widget);
}
and you are writing a function which may call set_widget(), it can save an unnecessary shared_ptr copy if you pass it as a const reference: void maybe_set_widget( Foo& foo, const shared_ptr<Widget>& widget) {
if(!foo.has_widget()) {
foo.set_widget(widget);
}
}
Doing a quick google search finds that Herb Sutter agrees with me (https://herbsutter.com/2013/06/05/gotw-91-solution-smart-poi...):>Use a const shared_ptr& as a parameter only if you’re not sure whether or not you’ll take a copy and share ownership
The main performance ‘problem’ with shared pointers is locking and contention in heavily multithreaded scenarios, which is why pretty much nobody uses a reference counting memory model for these kinds of things. In the vast majority of single or lightly threaded scenarios I think you will be hard pressed to find any reasonable algorithm implemented using shared pointers where the reference counting overhead is even measurable. So it’s not a strange thing at all to deprioritize hypothetical performance gains over clear intent of your code. Best thing is to stay away from shared pointers unless you really need them anyway, as they promote designs with sloppy ownership semantics. Been there, done that, try to get rid of them years later... :-/
The almost obligatory Herb Sutter reference made me chuckle by the way. He says so many things about what he thinks is the right thing to do in C++, and gets quoted almost as an absolute authority on good C++ practices, but quite often (not always, mind you) I find his arguments weak and unconvincing, and his recommendations sometimes downright counterproductive.
Could you elaborate? I'm not an expert in C++ but smart pointers did simplify a lot of my code and I never met the case where it was a problem.
Like everything else in C++ (and any low-level language), they should be used carefully.
If you don’t know what the lifetime of your objects is, it is very difficult to follow RAII best practices, to control memory use (a shared pointer that is held on to where it is not necessary is basically a leak), to avoid high and/or unpredictable allocation/deallocation cost (compared to e.g. arena allocators or just stack copies), etc. On top of that there is a very conceivable risk of introducing cycles (and hence leaks) when overusing shared pointers. And last but not least debugging code that defaults to shared pointers everywhere can be a nightmare.
These are all practical downsides, but to me personally the argument that you have to be explicit about your memory model (allocation, ownership, lifetime) and use the right tools for your design is even more important. Shared pointers are definitely a very useful tool, but only in situations where you truly want to have shared ownership, and not just because they are convenient because they save you from having to think about these things. Over time I’ve found that more often than not, using strictly unique_ptr for ownership, raw pointers and references to avoid copies, and plain value copies where appropriate and not performance critical, leads to code that is much easier to reason about, has less bugs, etc.
The pool dequeued a task, issued it to a thread, the thread then called a method on the object and asked for more work.
I stumbled over a case where the destructor of the task was not always called, leaving all sorts of handles hanging.
Turned out the code that dequeued a task did so into a local variable, and didn't clear that variable after it had passed it on to the thread. So when the pool ran out of work, a reference to the last task was being held by the local variable in the pool code, and that reference was only released when new work arrived or the pool was destroyed.
It is also very easy to accidentally create cyclic references with shared pointers. Suppose you have a class like this:
struct Node {
shared_ptr<Node> parent;
shared_ptr<Node> child;
};
This rather innocent looking class creates a cycle when you fill in the values, because the parent and child reference each other. That cycle now means your nodes will not be cleaned up anymore.Unique pointers do not have any of these problems. They cannot create cycles, are very cheap and they make it very explicit who owns what. They are not suitable for every situation (i.e. when there is actually shared ownership shared pointers should be used) but in most situations I find that they are sufficient and have a lot less pitfalls than shared pointers.
I don't think this is true. Couldn't you accidentally make two objects "own" each other?
It’s fairly common knowledge, but just to answer the inevitable question: you can avoid the cycle with weak pointers.
However, you missed a _super_ important caveat of this which is very subtle and very awful. And that is all of the code being executing here between the start of the function and the copy of the shared_ptr is doing it without the protection of the reference count on the shared pointer. It's entirely conceivable that, for example, the `has_widget` function, while executing, invalidates the original shared_ptr passed in and thus makes your eventual use of that shared_ptr invalid.
shared_ptr<int> global;
void func(const shared_ptr<int>& v) {
global.reset();
*v += 1; // ohno
}
func(global);/* XXX the argument might be the same as the global value we're about to modify. API designers, eh? */
I loathe when someone or some article rants on about their silver bullet solution, or alternatively, that doing some thing is always bad.
I learned a test taking strategy early in my life - when faced with a multiple choice question, if you see "always" or "never", that answer is usually incorrect.
To come back around to this article and similar software development articles - we have many tools in our toolbox, the trick is knowing when to use (or not use) a particular tool. Never throw your tools away.