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