Question

If I define an exception class

class Exception : public std::runtime_error
{
/*...*/
private:
    std::exception_ptr next;
    std::exception_ptr prev;
}

in which I refer to pending exceptions by next and new thrown exceptions while this is pending by prev, do the exception get freed after the exception is handled? The reason I'm asking is, that I once heard that std::exception_ptr is implemented in terms of reference counting which can lead to memory leaks, if there are reference cicles as I produced in this class.

Was it helpful?

Solution

The reason I'm asking is, that I once heard that std::exception_ptr is implemented in terms of reference counting [...]

Not necessarily, but it could be. Paragraph 18.8.5/6 of the C++11 Standard mentions this in a note:

typedef unspecified exception_ptr;

[...]

6 [ Note: An implementation might use a reference-counted smart pointer as exception_ptr. —end note ]

Therefore, you may not want to establish cycles of exception_ptr.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top