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.
This commit is contained in:
Mira Grudzinska 2021-11-06 09:59:29 +01:00 committed by Hermet Park
parent 6ba2bc8eb3
commit 14c1562b36
2 changed files with 9 additions and 6 deletions

View file

@ -99,12 +99,15 @@ Saver::~Saver()
Result Saver::save(std::unique_ptr<Paint> 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;

View file

@ -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;