This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 110c. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-02-07


1786. Effect of merging allocations on memory leakage

Section: 7.6.2.8  [expr.new]     Status: C++14     Submitter: United Kingdom     Date: 2013-09-28

N3690 comment GB 4

[Applied to WP at the February, 2014 meeting as part of document N3914.]

The strategy of merging allocations could turn a small memory leak into a big one. For example,

  class P {
    int x;
  };
  class Q {
  public:
    Q(){ throw 42; }
  private:
    int x[LARGE_NUMBER];
  };

  {
    P* p1 = new P();
    Q* q1 = new Q(); // bang :-( 
    // don't get here 
    delete q1;
    delete p1;
  }

Instead of just leaking the first allocation, this could result in leaking the combined allocation.

Notes from the September, 2013 meeting:

EWG was not so concerned with memory leaks, but there are problems with the current wording dealing with lifetime issues. In particular, the existing wording did not guarantee that all of the merged allocations would, in fact, be executed. The intended direction is to relax the strict lifetime containment requirement from the current wording but to ensure that all of the allocations and frees will be executed, which requires that the allocation and initialization of later objects be non-throwing.