Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++

exceptions and resource deallocation

 
Jump to:  
 
Vasilis
PostPosted: Tue Jul 29, 2008 4:23 pm    Post subject: 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.Cool
==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 LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Pete Becker
PostPosted: Tue Jul 29, 2008 8:59 pm    Post subject: Re: exceptions and resource deallocation
       
On 2008-07-29 06:23:35 -0400, Vasilis <VASILI3000@hotmail.com> said:

Quote:

#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


Nobody caught the exception, so the implementation calls
std::terminate(), and it's implementation-defined whether the stack is
unwound before that call. Add try and catch blocks to main, and catch
the exception.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)



[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Mathias Gaunard
PostPosted: Tue Jul 29, 2008 9:28 pm    Post subject: Re: exceptions and resource deallocation
       
On 29 juil, 18:23, Vasilis <VASILI3...@hotmail.com> wrote:
Quote:
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;}

The destructor of auto_ptr will not be called here, because the
program will abort before that.
Try

int main()
{
try
{
std::auto_ptr<dummy> temp(new dummy());
f(7);
}
catch(...)
{
std::cerr << "An unknown exception occured!" << std::endl;
}
}


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
David Abrahams
PostPosted: Wed Jul 30, 2008 5:32 am    Post subject: Re: exceptions and resource deallocation
       
on Tue Jul 29 2008, Vasilis <VASILI3000-AT-hotmail.com> wrote:

Quote:
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:

The rules say that if you let an exception escape your program without
catching it, the compiler is allowed (but not required) to skip
unwinding and any associated destructors. That's what's happening to
you.

HTH,

--
Dave Abrahams
BoostPro Computing
LINK

[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Vasilis
PostPosted: Wed Jul 30, 2008 10:28 am    Post subject: Re: exceptions and resource deallocation
       
On Jul 29, 9:32 pm, David Abrahams <d...@boostpro.com> wrote:
Quote:
The rules say that if you let an exception escape your program without
catching it, the compiler is allowed (but not required) to skip
unwinding and any associated destructors. That's what's happening to
you.
I didn't know that the compiler was not required to unwind the stack

on exit. true is that
if i catch the exception there is no memory leak.

thank you all,
Vasilis.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Ziaja Krasicki Ignacy wiersze Teka poker Do Samotnoƛci - Mickiewicz Adam