Re: On deletion of exception objects in C++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Debarshi,

> Could you please help me understand the issue?

Use one of two strategies:

- - - - - - - - - -
Strategy #1

Throw by value, not by reference.

Catch by reference.  Preferably const-reference, to help prevent slicing.

Use the rethrow statement (throw;) to rethrow the same inflight thrown
object, rather than "throw e;" which may create a copy that is not on the
heap.  [I think that's your crasher, since you are doing a delete on
something may not be on the heap.  The new'd object is being leaked.  Does
not crash on my system -- but that just may be happenstance.]

- - - - - - - - - -
Strategy #2

Throw by pointer.

Catch by pointer.

Delete the caught pointer at the appropriate catch block.

- - - - - - - - - -
Do not use the convention of creating a heap object and then throwing a
reference, with the expectation that you will delete the heap object later.

All sorts of things can disrupt that scheme.  That's a recipe for a crasher.

HTH,
--Eljay



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux