Properly clean up when NotCurses instance is destructed

We need to set `_instance` to `nullptr` or we'll leave a dangling
pointer.
This commit is contained in:
Marek Habersack 2020-01-25 18:45:35 +01:00 committed by Nick Black
parent ac791c781d
commit cd6d1e16d1
2 changed files with 13 additions and 9 deletions

View File

@ -31,17 +31,10 @@ namespace ncpp
explicit NotCurses (const notcurses_options &nc_opts, FILE *fp = nullptr);
// Must not move or copy a NotCurses instance because we have no way to guarantee validity of any other copy
// when even a single instance is destructed as that operation would close not curses.
// when even a single instance is destructed as that operation would close notcurses.
NotCurses (const NotCurses &other) = delete;
NotCurses (NotCurses &&other) = delete;
~NotCurses ()
{
if (nc == nullptr)
return;
notcurses_stop (nc);
}
~NotCurses ();
operator notcurses* () noexcept
{

View File

@ -17,6 +17,17 @@ notcurses_options NotCurses::default_notcurses_options = {
NotCurses *NotCurses::_instance = nullptr;
std::mutex NotCurses::init_mutex;
NotCurses::~NotCurses ()
{
const std::lock_guard<std::mutex> lock (init_mutex);
if (nc == nullptr)
return;
notcurses_stop (nc);
_instance = nullptr;
}
NotCurses::NotCurses (const notcurses_options &nc_opts, FILE *fp)
{
const std::lock_guard<std::mutex> lock (init_mutex);