[Rcpp-devel] Does uncaught c++ exception cause memory leak?
Dirk Eddelbuettel
edd at debian.org
Tue Dec 30 04:05:05 CET 2014
PS Here is a better variant that actually leaks:
edd at max:/tmp$ cat leak.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
int main() {
double *d = new double[1024];
if (d) std::cout << "Allocated" << std::endl;
double e = std::exp(1.0);
d = &e;
exit(0);
}
edd at max:/tmp$ g++ -Wall -o leak leak.cpp
edd at max:/tmp$ valgrind ./leak
==16924== Memcheck, a memory error detector
==16924== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==16924== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==16924== Command: ./leak
==16924==
Allocated
==16924==
==16924== HEAP SUMMARY:
==16924== in use at exit: 8,192 bytes in 1 blocks
==16924== total heap usage: 1 allocs, 0 frees, 8,192 bytes allocated
==16924==
==16924== LEAK SUMMARY:
==16924== definitely lost: 8,192 bytes in 1 blocks
==16924== indirectly lost: 0 bytes in 0 blocks
==16924== possibly lost: 0 bytes in 0 blocks
==16924== still reachable: 0 bytes in 0 blocks
==16924== suppressed: 0 bytes in 0 blocks
==16924== Rerun with --leak-check=full to see details of leaked memory
==16924==
==16924== For counts of detected and suppressed errors, rerun with: -v
==16924== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
edd at max:/tmp$
By overwriting d the memory is lost for good, and valgrind notices as you;d
think it would.
Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list