Is it possible to create a memory leak by using std::exception_ptr
Asked Answered
C

1

1

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.

Crawley answered 4/6, 2013 at 10:44 Comment(0)
Z
1

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.

Zebada answered 4/6, 2013 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.