4

I remember that Scott Meyers taught me that

func(shared_ptr(new P), shared_ptr(new Q));

is dangerous, because (if I remember correctly) the order of memory allocation, reference counting (constructing) and assignment to function parameters allows a leak (theoretically?) to appear in rare circumstances. To prevent this one should encapsulate the shared_ptr in a function call, e.g. in make_shared().

func(make_shared<P>(), make_shared<Q>());

Here is some discussion about it, too.

I would like to know if there are (current) compilers in the field, on certain systems that indeed may leave some hole in some error cases? Or are those times gone, or were they only theoretical, anyway?

Most interesting would be know if any of these have that issue:

  • g++ 4.x or g++ 2.95, on Linux i386, x64, ARM, m68k or any Windows
  • Visual C++ on i368, x64 or ARM
  • Clang/LLVM on Linux, or any of its platforms
  • How about C++ compilers on/from Sun or IBM, HP-UX?

Has anyone observed this behavior on his specific platform?

10
  • 1
    Note: To would-be answerers; the OP is aware of the potential leak, so further references to why it could leak are futile. The question is specific: did anyone witnessed it ? Aug 6, 2013 at 9:16
  • Well, I've never encountered an actual problem because of this, because I'd never write it in the first place. Any compiler doing any degree of optimization may break this code in specific cases. (Finding which cases is left as an exercise for the reader.) Aug 6, 2013 at 9:27
  • @MatthieuM. Which is a poor question, since if one is aware of the potential leak, one doesn't write such code, and so one won't witness it. What is certain is that the problem isn't theoretical, because compilers do reorder code in ways that would break the first example. It's part of the standard optimization techniques, used by practically every compiler. Aug 6, 2013 at 9:36
  • 2
    I would point that relying on the absence of use of this optimization is hazardous. This leads to massive breakdowns of software such as what happened when gcc 4.7 introduced a more aggressive reuse of stack space allocated for temporaries => many programs who relied on temporaries leaving longer than the Standard guaranteed suddenly broke down (in sometimes mysterious ways). Aug 6, 2013 at 9:37
  • @JamesKanze: I am aware of optimizations techniques, but I must admit I never witnessed the issue. I have seen many optimizations hampered by the presence of exceptions, for example, and I do not know whether this is the case here (or not). Certainly, even if today it works, it is unreliable. Aug 6, 2013 at 9:40

4 Answers 4

4

This isn't a platform problem, it's an exception-safety issue. So the answer to your actual question is: all those platforms might exhibit the issue.

The memory leak problem arises due to 2 things:

  • Allocating memory with new might throw bad_alloc
  • The order in which arguments to functions are evaluated is unspecified.

The docs for boost::shared_ptr capture it nicely here

There is more treatment in detail on the general problem here (GOTW)

The reason it might be "rare" comes about because it's really not that common to get bad_alloc, but your code must handle the possibility safely if it is to avoid memory leaks.

(I say "might" exhibit it - I haven't checked that they all throw bad_alloc if new fails...)

3
  • Not only bad_alloc of new is be problematic, but any exception thrown in the constructors P() or Q(), correct? So, depending on the implemented evaluation order of the function arguments, I would observe the memory leak in one of both cases, always? This would mean that the "unspecifiedness" of function arguments does not have anything to do with it -- it could be specified to, say, left-to-right and then I would observe the leak, still. Wouldn't I? I seem to remember that it is rather the "unspecifiedness" of alloc/new/param, and I wonder if "the right order" could remedy it.
    – towi
    Aug 6, 2013 at 8:58
  • Your answer is nice, but off-topic. The OP is aware of the issue already and asks on which compilers/platforms it was witnessed. Aug 6, 2013 at 9:14
  • @towi - you're right - any exception during construction of either could provoke it. The evaluation order being unspecified means that a compliant implementation can evaluate them in any order it likes as long as they're all /completely/ evaluated when the function body is entered. Tying this down to one specified order would remove an important optimisation point, and possibly make it difficult for some platforms to implement it at all.
    – SteveLove
    Aug 6, 2013 at 9:14
0
func(shared_ptr(new P), shared_ptr(new Q));

A C++ compiler is free to implement this in the following order:

  1. new Q
  2. new P
  3. construct shared_ptr around allocated P
  4. construct shared_ptr around allocated Q
  5. call func

(A compiler can perform 1, 2, 3, and 4 in any order, as long as 1 is before 4 and 2 before 3).

In the order above, if P's constructor or the call to new throws, Q is a memory leak (memory is allocated but the shared_ptr is not yet constructed around it).

As such, you should call std::make_shared (which handles exceptions on allocation gracefully), and you know that when std::make_shared has returned for one of them, the shared_ptr is fully constructed and will not leak.

I would like to know if there are (current) compilers in the field, on certain systems that indeed may leave some hole in some error cases?

All standards compliant compilers will have this behavior.

7
  • Would a compiler be standard compliant that implements the order 1,4,2,3,5 and would that solve the problem? First, doing all Q-work, then all P-work?
    – towi
    Aug 6, 2013 at 9:03
  • Yes, it would be compliant, but a compiler doesn't know what a shared_ptr is (other than a class). It doesn't know that it should create smart pointers before anything else.
    – utnapistim
    Aug 6, 2013 at 9:07
  • @utnapistim: The OP is aware of the issue, the question is about whether it is theoretical or if some compilers do exhibit the behavior (and which). Aug 6, 2013 at 9:15
  • 1
    @SteveL: I too love Standard compliance, however the OP did not ask whether the Standard allowed it (he knows it does), but asked whether someone knew of a compiler that exhibited this behavior. The answer here says that all standard compliant compilers may exhibit the behavior, which is certainly true, but falls short of answering the question asked. Aug 6, 2013 at 9:28
  • 1
    @towi: Look at -fstack-reuse code generation option in GCC. It was introduced to forbid the optimization and present a simple example. Aug 6, 2013 at 13:40
0

because both new operation will be done first, and then they are passed into shared_ptr's constructor, but which shared_ptr constructs first is unspecified, so one of the newly created object may cause a memory leak.

2
  • This sounds factual. Is this always "the order of things"? Or only "one possible order", and could another order prevent this leak?
    – towi
    Aug 6, 2013 at 9:00
  • @towi Many different orderings are possible, and which ordering will depend on the compiler, the options used to invoke it, and the surrounding context (how many registers are available, etc.). About all you can say is that most, if not all compilers will use an order which breaks the first line in certain cases. Aug 6, 2013 at 9:34
0

This is unsafe on any platform which has a reordering optimizer, if that optimizer performs the following optimization A;B;A;B => A;A;B;B. This optimization improves code cache efficiency, so in general that's a good idea.

Obviously that optimizer can only rearrange B and A if their relative order is unspecified, which happens to be the case here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.