http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57510

            Bug ID: 57510
           Summary: initializer_list memory leak
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matt at ookypooky dot com

Created attachment 30247
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30247&action=edit
Code demonstrating the problem

It looks as though initializer_list can suffer from memory leaks - if during
construction of the initializer_list members an exception is thrown then the
previously allocated members are not correctly destroyed. Unfortunately I do
not have access to a newer g++ version than 4.7.2, however I could not see any
mention of this issue in the closed bugs.

Code (also attached):
--
#include <memory>
#include <random>

struct X
{
    X () : m_n (std::unique_ptr<int> (new int)) { if (random () & 1) throw 1; }
    std::unique_ptr<int> m_n;
};

void foo (std::initializer_list<X>)
{
}

int main ()
{
  for (int i = 0; i < 10; ++i)
  {
    try
    {
      foo ({ X{}, X{} });
    }
    catch (...) {}
  }
}
--

$ g++ -v
Using built-in specs.
COLLECT_GCC=/software/thirdparty/gcc/4.7.2-0.el6_64/bin/g++
COLLECT_LTO_WRAPPER=/software/thirdparty/gcc/4.7.2-0.el6_64/libexec/gcc/x86_64-unknown-linux-gnu/4.7.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/software/thirdparty/gcc/4.7.2-0.el6_64
--with-system-zlib --enable-shared --enable-threads=posix --enable-laguages=all
Thread model: posix
gcc version 4.7.2 (GCC)

$ g++ -std=c++11 memory_leak.cpp

$ valgrind ./a.out
==13775== Memcheck, a memory error detector
==13775== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==13775== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==13775== Command: ./a.out
==13775==
==13775==
==13775== HEAP SUMMARY:
==13775==     in use at exit: 12 bytes in 3 blocks
==13775==   total heap usage: 23 allocs, 20 frees, 1,244 bytes allocated
==13775==
==13775== LEAK SUMMARY:
==13775==    definitely lost: 12 bytes in 3 blocks
==13775==    indirectly lost: 0 bytes in 0 blocks
==13775==      possibly lost: 0 bytes in 0 blocks
==13775==    still reachable: 0 bytes in 0 blocks
==13775==         suppressed: 0 bytes in 0 blocks
==13775== Rerun with --leak-check=full to see details of leaked memory
==13775==
==13775== For counts of detected and suppressed errors, rerun with: -v
==13775== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

Reply via email to