From 14c1562b36f6e3d2675438433df959752b650c84 Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Sat, 6 Nov 2021 09:59:29 +0100 Subject: [PATCH] tvg_saver: fix memory leaks In the cae when Result::InsufficientCondition was retured by the save() api, the user had to remember to delete the passed paint - fixed. Also path was not released. --- src/lib/tvgSaver.cpp | 9 ++++++--- src/savers/tvg/tvgTvgSaver.cpp | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/tvgSaver.cpp b/src/lib/tvgSaver.cpp index dc240d00..1a3e8614 100644 --- a/src/lib/tvgSaver.cpp +++ b/src/lib/tvgSaver.cpp @@ -99,12 +99,15 @@ Saver::~Saver() Result Saver::save(std::unique_ptr paint, const string& path, bool compress) noexcept { - //Already on saving an other resource. - if (pImpl->saveModule) return Result::InsufficientCondition; - auto p = paint.release(); if (!p) return Result::MemoryCorruption; + //Already on saving an other resource. + if (pImpl->saveModule) { + delete(p); + return Result::InsufficientCondition; + } + if (auto saveModule = _find(path)) { if (saveModule->save(p, path, compress)) { pImpl->saveModule = saveModule; diff --git a/src/savers/tvg/tvgTvgSaver.cpp b/src/savers/tvg/tvgTvgSaver.cpp index d7c97987..f1648fb8 100644 --- a/src/savers/tvg/tvgTvgSaver.cpp +++ b/src/savers/tvg/tvgTvgSaver.cpp @@ -748,9 +748,6 @@ bool TvgSaver::save(Paint* paint, const string& path, bool compress) { close(); - this->path = strdup(path.c_str()); - if (!this->path) return false; - float x, y; x = y = 0; paint->bounds(&x, &y, &vsize[0], &vsize[1], false); @@ -764,6 +761,9 @@ bool TvgSaver::save(Paint* paint, const string& path, bool compress) return false; } + this->path = strdup(path.c_str()); + if (!this->path) return false; + this->paint = paint; this->compress = compress;