Am I doing something wrong here? Or is it really a memory leak in STL?... #include <string> #include <vector> int max = 4000000; void bad() { std::vector<std::string*> v; for (int x = 0; x < max; x++) { v.push_back(new std::string("test")); } for (uint x = 0; x < max; x++) { delete v.at(x); } } void good() { std::vector<std::string*> v; for (int x = 0; x < max; x++) { v.push_back(new std::string("test")); delete v.at(x); } } int main(int argc, char *argv[]) { bad(); // this is not good... //good(); // this works perfectly //usleep(20000 * 1000); // stop here so that we have time to check the RSS return 0; }
If you check the RSS before exiting (enable usleep) you see that when running bad() the heap size of the process doesn't decrease (stays aroud ~300Mbs for me). Compiled as: g++ test.cpp -o test gcc -v: Reading specs from /usr/lib/gcc/x86_64-pc-linux-gnu/3.4.4/specs Configured with: /var/tmp/portage/gcc-3.4.4-r1/work/gcc-3.4.4/configure --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/3.4.4 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/3.4.4/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/3.4.4 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/3.4.4/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/3.4.4/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/3.4.4/include/g++-v3 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --enable-multilib --disable-libgcj --enable-languages=c,c++,f77 --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu Thread model: posix gcc version 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8) -- Summary: Memory leak in std::string?.. Product: gcc Version: 3.4.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: sakovacs at freemail dot hu GCC build triplet: x86_64-pc-linux-gnu GCC host triplet: x86_64-pc-linux-gnu GCC target triplet: x86_64-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27079