exceptions and resource deallocation
Dear list,
I have problems understanding what exactly goes on during stack
unwinding and resource deallocation. Here is my sample code:
#include <memory>
class exception{};
class dummy{};
void f ( int a ) {
if ( a == 7 )
throw exception();
}
int main() {
std::auto_ptr<dummy> temp( new dummy() );
f ( 7 );
return 1;
}
//sample code end
Now, i must admit, with great surprise, feeding this to valgrind, i
get back a byte leaked, besides a mysterious error that has to do with
the exception throwing itself:
==15040== 1 bytes in 1 blocks are still reachable in loss record 1 of
2
==15040== at 0x4022AA7: operator new(unsigned) (vg_replace_malloc.c:
163)
==15040== by 0x80485FB: main (example.cpp:13)
==15040==
==15040==
==15040== 81 bytes in 1 blocks are still reachable in loss record 2 of
2
==15040== at 0x402239B: malloc (vg_replace_malloc.c:149)
==15040== by 0x40EF371: __cxa_allocate_exception (in /usr/lib/
libstdc++.so.6.0.8)
==15040== by 0x80485C9: f(int) (example.cpp:9)
==15040== by 0x8048618: main (example.cpp:14)
==15040==
==15040== LEAK SUMMARY:
==15040== definitely lost: 0 bytes in 0 blocks.
==15040== possibly lost: 0 bytes in 0 blocks.
==15040== still reachable: 82 bytes in 2 blocks.
==15040== suppressed: 0 bytes in 0 blocks.
//valgrind output terminates here.
I am not worried at all by the second leak. The first one surprises
me.
Isn't this all about smart pointers? resource deallocation on
exception throwing? What's wrong?
Thank you all,
vasilis.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]