Monitor: avoid null pointer dereference when copying instances

When the copy constructor was added to allow passing a monitor by value
into a lambda the implementation did not account for the possibility of
the watchable having already been destroyed.

Also provide the companion copy assignment to complete the triad.
This commit is contained in:
Peter A. Bigot 2018-04-26 06:06:41 -05:00
parent adf4fb3bc1
commit 94bff62986
1 changed files with 19 additions and 1 deletions

View File

@ -64,7 +64,25 @@ public:
Monitor(const Monitor &monitor) : _watchable(monitor._watchable)
{
// register with the watchable
_watchable->add(this);
if (_watchable) _watchable->add(this);
}
/**
* Assignment operator
* @param monitor
*/
Monitor& operator= (const Monitor &monitor)
{
// remove from watchable
if (_watchable) _watchable->remove(this);
// replace watchable
_watchable = monitor._watchable;
// register with the watchable
if (_watchable) _watchable->add(this);
return *this;
}
/**