mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Fix memory leak when exception is thrown by impl classes in
Serial::read() vector and string variants.
This commit is contained in:
parent
fba8d81b5d
commit
31c16ff9e8
@ -132,7 +132,16 @@ Serial::read (std::vector<uint8_t> &buffer, size_t size)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
uint8_t *buffer_ = new uint8_t[size];
|
||||
size_t bytes_read = this->pimpl_->read (buffer_, size);
|
||||
size_t bytes_read = 0;
|
||||
|
||||
try {
|
||||
bytes_read = this->pimpl_->read (buffer_, size);
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
delete[] buffer_;
|
||||
throw;
|
||||
}
|
||||
|
||||
buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
|
||||
delete[] buffer_;
|
||||
return bytes_read;
|
||||
@ -143,7 +152,14 @@ Serial::read (std::string &buffer, size_t size)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
uint8_t *buffer_ = new uint8_t[size];
|
||||
size_t bytes_read = this->pimpl_->read (buffer_, size);
|
||||
size_t bytes_read = 0;
|
||||
try {
|
||||
bytes_read = this->pimpl_->read (buffer_, size);
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
delete[] buffer_;
|
||||
throw;
|
||||
}
|
||||
buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read);
|
||||
delete[] buffer_;
|
||||
return bytes_read;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user