fix memory leak when exception thrown

pull/1777/head
xzvno 2021-06-21 14:31:01 +08:00
parent faa6955c8d
commit 1e48d35f3b
2 changed files with 15 additions and 4 deletions

View File

@ -347,8 +347,13 @@ void Piece::updateWrCache(WrDiskCache* diskCache, unsigned char* data,
cell->len = len;
cell->capacity = capacity;
bool rv;
rv = wrCache_->cacheData(cell);
assert(rv);
try {
rv = wrCache_->cacheData(cell);
assert(rv);
} catch (RecoverableException& e) {
delete cell;
throw;
}
rv = diskCache->update(wrCache_.get(), len);
assert(rv);
}

View File

@ -41,6 +41,7 @@
#include "Segment.h"
#include "WrDiskCache.h"
#include "Piece.h"
#include "DlAbortEx.h"
namespace aria2 {
@ -81,8 +82,13 @@ ssize_t SinkStreamFilter::transform(const std::shared_ptr<BinaryStream>& out,
size_t capacity = std::max(len, static_cast<size_t>(4_k));
auto dataCopy = new unsigned char[capacity];
memcpy(dataCopy, inbuf + alen, len);
piece->updateWrCache(wrDiskCache_, dataCopy, 0, len, capacity,
segment->getPositionToWrite() + alen);
try {
piece->updateWrCache(wrDiskCache_, dataCopy, 0, len, capacity,
segment->getPositionToWrite() + alen);
} catch (RecoverableException& e) {
delete[] dataCopy;
throw;
}
}
}
else {