get rid of -fanalyzer memory leaks

Don't use make_shared inside a function. Instead, change constructor to
value to have std::move.

Also move shared_ptrs everywhere. It's fairly expensive to copy.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
main
Rosen Penev 3 years ago
parent d458bf2540
commit f981c51eea

@ -212,16 +212,16 @@ int TiffEntryBase::idx() const {
return idx_; return idx_;
} }
void TiffEntryBase::setData(const std::shared_ptr<DataBuf>& buf) { void TiffEntryBase::setData(std::shared_ptr<DataBuf> buf) {
storage_ = buf; storage_ = std::move(buf);
pData_ = buf->data(); pData_ = storage_->data();
size_ = buf->size(); size_ = storage_->size();
} }
void TiffEntryBase::setData(byte* pData, size_t size, const std::shared_ptr<DataBuf>& storage) { void TiffEntryBase::setData(byte* pData, size_t size, std::shared_ptr<DataBuf> storage) {
pData_ = pData; pData_ = pData;
size_ = size; size_ = size;
storage_ = storage; storage_ = std::move(storage);
if (!pData_) if (!pData_)
size_ = 0; size_ = 0;
} }
@ -231,7 +231,8 @@ void TiffEntryBase::updateValue(Value::UniquePtr value, ByteOrder byteOrder) {
return; return;
size_t newSize = value->size(); size_t newSize = value->size();
if (newSize > size_) { if (newSize > size_) {
setData(std::make_shared<DataBuf>(newSize)); auto d = std::make_shared<DataBuf>(newSize);
setData(std::move(d));
} }
if (pData_) { if (pData_) {
memset(pData_, 0x0, size_); memset(pData_, 0x0, size_);
@ -431,7 +432,8 @@ size_t TiffBinaryArray::addElement(size_t idx, const ArrayDef& def) {
// The assertion typically fails if a component is not configured in // The assertion typically fails if a component is not configured in
// the TIFF structure table (TiffCreator::tiffTreeStruct_) // the TIFF structure table (TiffCreator::tiffTreeStruct_)
tp->setStart(pData() + idx); tp->setStart(pData() + idx);
tp->setData(const_cast<byte*>(pData() + idx), sz, storage()); auto s = storage();
tp->setData(const_cast<byte*>(pData() + idx), sz, std::move(s));
tp->setElDef(def); tp->setElDef(def);
tp->setElByteOrder(cfg()->byteOrder_); tp->setElByteOrder(cfg()->byteOrder_);
addChild(std::move(tc)); addChild(std::move(tc));

@ -428,13 +428,13 @@ class TiffEntryBase : public TiffComponent {
you should pass std::shared_ptr<DataBuf>(), which is essentially you should pass std::shared_ptr<DataBuf>(), which is essentially
a nullptr. a nullptr.
*/ */
void setData(byte* pData, size_t size, const std::shared_ptr<DataBuf>& storage); void setData(byte* pData, size_t size, std::shared_ptr<DataBuf> storage);
/*! /*!
@brief Set the entry's data buffer. A shared_ptr is used to manage the DataBuf @brief Set the entry's data buffer. A shared_ptr is used to manage the DataBuf
because TiffEntryBase has a clone method so it is possible (in theory) for because TiffEntryBase has a clone method so it is possible (in theory) for
the DataBuf to have multiple owners. the DataBuf to have multiple owners.
*/ */
void setData(const std::shared_ptr<DataBuf>& buf); void setData(std::shared_ptr<DataBuf> buf);
/*! /*!
@brief Update the value. Takes ownership of the pointer passed in. @brief Update the value. Takes ownership of the pointer passed in.
@ -534,7 +534,7 @@ class TiffEntryBase : public TiffComponent {
static size_t writeOffset(byte* buf, size_t offset, TiffType tiffType, ByteOrder byteOrder); static size_t writeOffset(byte* buf, size_t offset, TiffType tiffType, ByteOrder byteOrder);
//! Used (internally) to create another reference to the DataBuf reference by storage_. //! Used (internally) to create another reference to the DataBuf reference by storage_.
[[nodiscard]] const std::shared_ptr<DataBuf>& storage() const { [[nodiscard]] std::shared_ptr<DataBuf> storage() const {
return storage_; return storage_;
} }

@ -1329,7 +1329,8 @@ void TiffReader::readTiffEntry(TiffEntryBase* object) {
v->read(pData, size, byteOrder()); v->read(pData, size, byteOrder());
object->setValue(std::move(v)); object->setValue(std::move(v));
object->setData(pData, size, std::make_shared<DataBuf>()); auto d = std::make_shared<DataBuf>();
object->setData(pData, size, std::move(d));
object->setOffset(offset); object->setOffset(offset);
object->setIdx(nextIdx(object->group())); object->setIdx(nextIdx(object->group()));
} catch (std::overflow_error&) { } catch (std::overflow_error&) {
@ -1374,7 +1375,7 @@ void TiffReader::visitBinaryArray(TiffBinaryArray* object) {
size_t size = object->TiffEntryBase::doSize(); size_t size = object->TiffEntryBase::doSize();
auto buf = std::make_shared<DataBuf>(cryptFct(object->tag(), pData, size, pRoot_)); auto buf = std::make_shared<DataBuf>(cryptFct(object->tag(), pData, size, pRoot_));
if (!buf->empty()) if (!buf->empty())
object->setData(buf); object->setData(std::move(buf));
} }
const ArrayDef* defs = object->def(); const ArrayDef* defs = object->def();

Loading…
Cancel
Save