Merge pull request #230 from xqing2003/patch-1

fix memory leak
This commit is contained in:
Emiel Bruijntjes 2018-06-10 20:40:20 +02:00 committed by GitHub
commit a3a6a27efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 2 deletions

View File

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