solve memory leak

This commit is contained in:
xqing2003 2018-06-01 23:30:30 +08:00 committed by GitHub
parent 9aafe15620
commit 7737917886
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 3 deletions

View File

@ -70,13 +70,20 @@ void TcpConnection::process(int fd, int flags)
auto *result = _state->process(monitor, fd, flags);
// are we still valid
if (!monitor.valid()) return;
if (!monitor.valid())
{
delete result;
return;
}
// skip if the same state is continued to be used, or when the process()
// method returns nullptr (which only happens when the object is destructed,
// and "this" is no longer valid)
if (!result || result == _state.get()) return;
if (!result || result == _state.get())
{
delete result;
return;
}
// replace it with the new implementation
_state.reset(result);
}