Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart pointers and parameter list allocation rules

An MSDN page about smart pointers includes a promoted warning about creating smart pointers in parameter lists:

Always create smart pointers on a separate line of code, never in a parameter list, so that a subtle resource leak won’t occur due to certain parameter list allocation rules.

What are the parameter list allocation rules that it is referring to? Under what circumstances can the resource leak occur?

like image 431
fo_ Avatar asked Sep 06 '25 03:09

fo_


2 Answers

It's referring to the possibility of evaluating parameters in a different order, e.g.

func(unique_ptr<MyClass>(new MyClass), a(), b());

if the order of evaluation is: a(), MyClass(), b(), then unique_ptr is constructed, it might happen that b() throws and memory will be leaked.

A safeguard (that has been added in C++14 and it's also more efficient) is to use make_unique (assuming MSVC and according to your compiler version, you might have to define one yourself or take a look here). The same applies to shared_ptr.

Take a look at the notes for std::make_shared here, too:

Moreover, code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g throws an exception because g() may be called after new int(42) and before the constructor of shared_ptr<int>. This doesn't occur in f(std::make_shared<int>(42), g()), since two function calls are never interleaved.

like image 58
Marco A. Avatar answered Sep 07 '25 19:09

Marco A.


If you do this:

func(shared_ptr<Foo>(new Foo), shared_ptr<Bar>(new Bar));

And the signature is:

void func(shared_ptr<Foo>, shared_ptr<Bar>);

What will happen if one of the constructors throws? It may happen that new has been called once successfully and then the other one fails (you don't know which one will get called first). If that happens, one of the objects could be leaked, because it was never held by a resource manager.

You can read more here: http://www.gotw.ca/gotw/056.htm

like image 42
John Zwinck Avatar answered Sep 07 '25 20:09

John Zwinck