mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-14 04:19:12 +00:00
Fix minor memory leaks loading palettes using std::unique_ptrs
This commit is contained in:
parent
ab4088502c
commit
8a7f6930d0
@ -52,11 +52,12 @@ base::paths get_writable_palette_extensions()
|
||||
return paths;
|
||||
}
|
||||
|
||||
Palette* load_palette(const char* filename,
|
||||
const FileOpConfig* config)
|
||||
std::unique_ptr<doc::Palette> load_palette(
|
||||
const char* filename,
|
||||
const FileOpConfig* config)
|
||||
{
|
||||
dio::FileFormat dioFormat = dio::detect_format(filename);
|
||||
Palette* pal = nullptr;
|
||||
std::unique_ptr<Palette> pal = nullptr;
|
||||
|
||||
switch (dioFormat) {
|
||||
|
||||
@ -100,7 +101,7 @@ Palette* load_palette(const char* filename,
|
||||
if (fop->document() &&
|
||||
fop->document()->sprite() &&
|
||||
fop->document()->sprite()->palette(frame_t(0))) {
|
||||
pal = new Palette(
|
||||
pal = std::make_unique<Palette>(
|
||||
*fop->document()->sprite()->palette(frame_t(0)));
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2019 Igara Studio S.A.
|
||||
// Copyright (C) 2019-2022 Igara Studio S.A.
|
||||
// Copyright (C) 2001-2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -12,6 +12,8 @@
|
||||
#include "base/paths.h"
|
||||
#include "gfx/color_space.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
class Palette;
|
||||
}
|
||||
@ -22,8 +24,9 @@ namespace app {
|
||||
base::paths get_readable_palette_extensions();
|
||||
base::paths get_writable_palette_extensions();
|
||||
|
||||
doc::Palette* load_palette(const char *filename,
|
||||
const FileOpConfig* config = nullptr);
|
||||
std::unique_ptr<doc::Palette> load_palette(
|
||||
const char *filename,
|
||||
const FileOpConfig* config = nullptr);
|
||||
bool save_palette(const char *filename,
|
||||
const doc::Palette* pal,
|
||||
int columns,
|
||||
|
@ -52,7 +52,7 @@ void load_default_palette()
|
||||
// If there is no palette in command line, we use the default one.
|
||||
std::string palFile = defaultPalName;
|
||||
if (base::is_file(palFile)) {
|
||||
pal.reset(load_palette(palFile.c_str()));
|
||||
pal = load_palette(palFile.c_str());
|
||||
}
|
||||
else {
|
||||
// Migrate old default.gpl to default.ase format
|
||||
@ -60,7 +60,7 @@ void load_default_palette()
|
||||
get_default_palette_preset_name(), ".gpl");
|
||||
|
||||
if (base::is_file(palFile)) {
|
||||
pal.reset(load_palette(palFile.c_str()));
|
||||
pal = load_palette(palFile.c_str());
|
||||
|
||||
// Remove duplicate black entries at the end (as old palettes
|
||||
// contains 256 colors)
|
||||
@ -104,7 +104,7 @@ void load_default_palette()
|
||||
if (path.empty())
|
||||
path = App::instance()->extensions().palettePath("VGA 13h");
|
||||
if (!path.empty())
|
||||
pal.reset(load_palette(path.c_str()));
|
||||
pal = load_palette(path.c_str());
|
||||
}
|
||||
|
||||
// Save default.ase file
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2022 Igara Studio S.A.
|
||||
// Copyright (C) 2001-2017 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -9,6 +10,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "app/res/resource.h"
|
||||
#include "doc/palette.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
class Palette;
|
||||
@ -20,20 +24,20 @@ namespace app {
|
||||
public:
|
||||
PaletteResource(const std::string& id,
|
||||
const std::string& path,
|
||||
doc::Palette* palette)
|
||||
std::unique_ptr<doc::Palette>&& palette)
|
||||
: m_id(id)
|
||||
, m_path(path)
|
||||
, m_palette(palette) {
|
||||
, m_palette(std::move(palette)) {
|
||||
}
|
||||
virtual ~PaletteResource() { }
|
||||
virtual const std::string& id() const override { return m_id; }
|
||||
virtual const std::string& path() const override { return m_path; }
|
||||
virtual doc::Palette* palette() { return m_palette; }
|
||||
virtual const doc::Palette* palette() { return m_palette.get(); }
|
||||
|
||||
private:
|
||||
std::string m_id;
|
||||
std::string m_path;
|
||||
doc::Palette* m_palette;
|
||||
std::unique_ptr<doc::Palette> m_palette;
|
||||
};
|
||||
|
||||
} // namespace app
|
||||
|
@ -63,9 +63,9 @@ void PalettesLoaderDelegate::getResourcesPaths(std::map<std::string, std::string
|
||||
Resource* PalettesLoaderDelegate::loadResource(const std::string& id,
|
||||
const std::string& path)
|
||||
{
|
||||
doc::Palette* palette = load_palette(path.c_str(), &m_config);
|
||||
auto palette = load_palette(path.c_str(), &m_config);
|
||||
if (palette)
|
||||
return new PaletteResource(id, path, palette);
|
||||
return new PaletteResource(id, path, std::move(palette));
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -20,8 +20,8 @@
|
||||
|
||||
namespace app {
|
||||
|
||||
ResourcesLoader::ResourcesLoader(ResourcesLoaderDelegate* delegate)
|
||||
: m_delegate(delegate)
|
||||
ResourcesLoader::ResourcesLoader(std::unique_ptr<ResourcesLoaderDelegate>&& delegate)
|
||||
: m_delegate(std::move(delegate))
|
||||
, m_done(false)
|
||||
, m_cancel(false)
|
||||
, m_thread(new base::thread([this]{ threadLoadResources(); }))
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2022 Igara Studio S.A.
|
||||
// Copyright (C) 2001-2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -20,7 +21,7 @@ namespace app {
|
||||
|
||||
class ResourcesLoader {
|
||||
public:
|
||||
ResourcesLoader(ResourcesLoaderDelegate* delegate);
|
||||
ResourcesLoader(std::unique_ptr<ResourcesLoaderDelegate>&& delegate);
|
||||
~ResourcesLoader();
|
||||
|
||||
void cancel();
|
||||
@ -34,7 +35,7 @@ namespace app {
|
||||
|
||||
typedef base::concurrent_queue<Resource*> Queue;
|
||||
|
||||
ResourcesLoaderDelegate* m_delegate;
|
||||
std::unique_ptr<ResourcesLoaderDelegate> m_delegate;
|
||||
bool m_done;
|
||||
bool m_cancel;
|
||||
Queue m_queue;
|
||||
|
@ -80,9 +80,9 @@ int Palette_new(lua_State* L)
|
||||
return luaL_error(L, "script doesn't have access to open file %s",
|
||||
absFn.c_str());
|
||||
|
||||
Palette* pal = load_palette(absFn.c_str());
|
||||
auto pal = load_palette(absFn.c_str());
|
||||
if (pal)
|
||||
push_new<PaletteObj>(L, nullptr, pal);
|
||||
push_new<PaletteObj>(L, nullptr, pal.release());
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
@ -109,9 +109,9 @@ int Palette_new(lua_State* L)
|
||||
return luaL_error(L, "script doesn't have access to open file %s",
|
||||
absFn.c_str());
|
||||
|
||||
Palette* pal = load_palette(absFn.c_str());
|
||||
auto pal = load_palette(absFn.c_str());
|
||||
if (pal)
|
||||
push_new<PaletteObj>(L, nullptr, pal);
|
||||
push_new<PaletteObj>(L, nullptr, pal.release());
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
|
@ -189,7 +189,7 @@ ColorPopup::ColorPopup(const ColorButtonOptions& options)
|
||||
ResourceFinder rf;
|
||||
rf.includeDataDir("palettes/tags.gpl");
|
||||
if (rf.findFirst())
|
||||
g_simplePal.reset(load_palette(rf.filename().c_str()));
|
||||
g_simplePal = load_palette(rf.filename().c_str());
|
||||
}
|
||||
|
||||
if (g_simplePal)
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2020 Igara Studio S.A.
|
||||
// Copyright (C) 2020-2022 Igara Studio S.A.
|
||||
// Copyright (C) 2001-2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -69,7 +69,7 @@ void PalettePopup::showPopup(const gfx::Rect& bounds)
|
||||
openWindowInForeground();
|
||||
}
|
||||
|
||||
void PalettePopup::onPalChange(doc::Palette* palette)
|
||||
void PalettePopup::onPalChange(const doc::Palette* palette)
|
||||
{
|
||||
const bool state =
|
||||
(UIContext::instance()->activeDocument() &&
|
||||
@ -105,7 +105,7 @@ void PalettePopup::onSearchChange()
|
||||
|
||||
void PalettePopup::onLoadPal()
|
||||
{
|
||||
doc::Palette* palette = m_paletteListBox.selectedPalette();
|
||||
const doc::Palette* palette = m_paletteListBox.selectedPalette();
|
||||
if (!palette)
|
||||
return;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2022 Igara Studio S.A.
|
||||
// Copyright (C) 2001-2017 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -29,7 +30,7 @@ namespace app {
|
||||
void showPopup(const gfx::Rect& bounds);
|
||||
|
||||
protected:
|
||||
void onPalChange(doc::Palette* palette);
|
||||
void onPalChange(const doc::Palette* palette);
|
||||
void onSearchChange();
|
||||
void onLoadPal();
|
||||
void onOpenFolder();
|
||||
|
@ -118,7 +118,9 @@ private:
|
||||
};
|
||||
|
||||
PalettesListBox::PalettesListBox()
|
||||
: ResourcesListBox(new ResourcesLoader(new PalettesLoaderDelegate))
|
||||
: ResourcesListBox(
|
||||
new ResourcesLoader(
|
||||
std::make_unique<PalettesLoaderDelegate>()))
|
||||
{
|
||||
addChild(&m_tooltips);
|
||||
|
||||
@ -130,7 +132,7 @@ PalettesListBox::PalettesListBox()
|
||||
[this]{ reload(); });
|
||||
}
|
||||
|
||||
doc::Palette* PalettesListBox::selectedPalette()
|
||||
const doc::Palette* PalettesListBox::selectedPalette()
|
||||
{
|
||||
Resource* resource = selectedResource();
|
||||
if (!resource)
|
||||
@ -146,14 +148,14 @@ ResourceListItem* PalettesListBox::onCreateResourceItem(Resource* resource)
|
||||
|
||||
void PalettesListBox::onResourceChange(Resource* resource)
|
||||
{
|
||||
doc::Palette* palette = static_cast<PaletteResource*>(resource)->palette();
|
||||
const doc::Palette* palette = static_cast<PaletteResource*>(resource)->palette();
|
||||
PalChange(palette);
|
||||
}
|
||||
|
||||
void PalettesListBox::onPaintResource(Graphics* g, gfx::Rect& bounds, Resource* resource)
|
||||
{
|
||||
auto theme = SkinTheme::get(this);
|
||||
doc::Palette* palette = static_cast<PaletteResource*>(resource)->palette();
|
||||
const doc::Palette* palette = static_cast<PaletteResource*>(resource)->palette();
|
||||
os::Surface* tick = theme->parts.checkSelected()->bitmap(0);
|
||||
|
||||
// Draw tick (to say "this palette matches the active sprite
|
||||
|
@ -22,9 +22,9 @@ namespace app {
|
||||
public:
|
||||
PalettesListBox();
|
||||
|
||||
doc::Palette* selectedPalette();
|
||||
const doc::Palette* selectedPalette();
|
||||
|
||||
obs::signal<void(doc::Palette*)> PalChange;
|
||||
obs::signal<void(const doc::Palette*)> PalChange;
|
||||
|
||||
protected:
|
||||
virtual ResourceListItem* onCreateResourceItem(Resource* resource) override;
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2020 Igara Studio S.A.
|
||||
// Copyright (c) 2020-2022 Igara Studio S.A.
|
||||
// Copyright (c) 2001-2018 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -25,7 +25,7 @@ using namespace base::serialization::big_endian;
|
||||
|
||||
const int ActMaxColors = 256;
|
||||
|
||||
Palette* load_act_file(const char *filename)
|
||||
std::unique_ptr<Palette> load_act_file(const char *filename)
|
||||
{
|
||||
std::ifstream f(FSTREAM_PATH(filename), std::ios::binary);
|
||||
if (f.bad())
|
||||
@ -52,7 +52,7 @@ Palette* load_act_file(const char *filename)
|
||||
pal->setEntry(i, rgba(r, g, b, 255));
|
||||
}
|
||||
|
||||
return pal.release();
|
||||
return pal;
|
||||
}
|
||||
|
||||
bool save_act_file(const Palette *pal, const char *filename)
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2022 Igara Studio S.A.
|
||||
// Copyright (c) 2001-2015 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -8,13 +9,15 @@
|
||||
#define DOC_FILE_ACT_FILE_H_INCLUDED
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
|
||||
class Palette;
|
||||
|
||||
namespace file {
|
||||
|
||||
Palette* load_act_file(const char* filename);
|
||||
std::unique_ptr<Palette> load_act_file(const char* filename);
|
||||
bool save_act_file(const Palette* pal, const char* filename);
|
||||
|
||||
} // namespace file
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
#define PROCOL_MAGIC_NUMBER 0xB123
|
||||
|
||||
@ -26,15 +27,13 @@ namespace file {
|
||||
using namespace base;
|
||||
|
||||
// Loads a COL file (Animator and Animator Pro format)
|
||||
Palette* load_col_file(const char* filename)
|
||||
std::unique_ptr<Palette> load_col_file(const char* filename)
|
||||
{
|
||||
Palette *pal = NULL;
|
||||
int c, r, g, b;
|
||||
FILE* f;
|
||||
|
||||
f = std::fopen(filename, "rb");
|
||||
FILE* f = std::fopen(filename, "rb");
|
||||
if (!f)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
|
||||
// Get file size.
|
||||
std::fseek(f, 0, SEEK_END);
|
||||
@ -49,8 +48,9 @@ Palette* load_col_file(const char* filename)
|
||||
}
|
||||
|
||||
// Animator format
|
||||
std::unique_ptr<Palette> pal = nullptr;
|
||||
if (!pro) {
|
||||
pal = new Palette(frame_t(0), 256);
|
||||
pal = std::make_unique<Palette>(frame_t(0), 256);
|
||||
|
||||
for (c=0; c<256; c++) {
|
||||
r = fgetc(f);
|
||||
@ -75,10 +75,10 @@ Palette* load_col_file(const char* filename)
|
||||
// Unknown format
|
||||
if (magic != PROCOL_MAGIC_NUMBER || version != 0) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
pal = new Palette(frame_t(0), std::min(d.quot, 256));
|
||||
pal = std::make_unique<Palette>(frame_t(0), std::min(d.quot, 256));
|
||||
|
||||
for (c=0; c<pal->size(); c++) {
|
||||
r = fgetc(f);
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2022 Igara Studio S.A.
|
||||
// Copyright (c) 2001-2014 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -8,13 +9,15 @@
|
||||
#define DOC_FILE_COL_FILE_H_INCLUDED
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
|
||||
class Palette;
|
||||
|
||||
namespace file {
|
||||
|
||||
Palette* load_col_file(const char* filename);
|
||||
std::unique_ptr<Palette> load_col_file(const char* filename);
|
||||
bool save_col_file(const Palette* pal, const char* filename);
|
||||
|
||||
} // namespace file
|
||||
|
@ -26,7 +26,7 @@
|
||||
namespace doc {
|
||||
namespace file {
|
||||
|
||||
Palette* load_gpl_file(const char* filename)
|
||||
std::unique_ptr<Palette> load_gpl_file(const char* filename)
|
||||
{
|
||||
std::ifstream f(FSTREAM_PATH(filename));
|
||||
if (f.bad()) return NULL;
|
||||
@ -98,7 +98,7 @@ Palette* load_gpl_file(const char* filename)
|
||||
pal->setComment(comment);
|
||||
}
|
||||
|
||||
return pal.release();
|
||||
return pal;
|
||||
}
|
||||
|
||||
bool save_gpl_file(const Palette* pal, const char* filename)
|
||||
|
@ -8,13 +8,15 @@
|
||||
#define DOC_FILE_GPL_FILE_H_INCLUDED
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
|
||||
class Palette;
|
||||
|
||||
namespace file {
|
||||
|
||||
Palette* load_gpl_file(const char* filename);
|
||||
std::unique_ptr<Palette> load_gpl_file(const char* filename);
|
||||
bool save_gpl_file(const Palette* pal, const char* filename);
|
||||
|
||||
} // namespace file
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2022 Igara Studio S.A.
|
||||
// Copyright (c) 2016-2018 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -23,7 +24,7 @@
|
||||
namespace doc {
|
||||
namespace file {
|
||||
|
||||
Palette* load_hex_file(const char *filename)
|
||||
std::unique_ptr<Palette> load_hex_file(const char *filename)
|
||||
{
|
||||
std::ifstream f(FSTREAM_PATH(filename));
|
||||
if (f.bad())
|
||||
@ -64,7 +65,7 @@ Palette* load_hex_file(const char *filename)
|
||||
}
|
||||
}
|
||||
|
||||
return pal.release();
|
||||
return pal;
|
||||
}
|
||||
|
||||
bool save_hex_file(const Palette *pal, const char *filename)
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2022 Igara Studio S.A.
|
||||
// Copyright (c) 2016 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -8,13 +9,15 @@
|
||||
#define DOC_FILE_HEX_FILE_H_INCLUDED
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
|
||||
class Palette;
|
||||
|
||||
namespace file {
|
||||
|
||||
Palette* load_hex_file(const char* filename);
|
||||
std::unique_ptr<Palette> load_hex_file(const char* filename);
|
||||
bool save_hex_file(const Palette* pal, const char* filename);
|
||||
|
||||
} // namespace file
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2022 Igara Studio S.A.
|
||||
// Copyright (c) 2001-2018 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -24,7 +25,7 @@
|
||||
namespace doc {
|
||||
namespace file {
|
||||
|
||||
Palette* load_pal_file(const char *filename)
|
||||
std::unique_ptr<Palette> load_pal_file(const char *filename)
|
||||
{
|
||||
std::ifstream f(FSTREAM_PATH(filename));
|
||||
if (f.bad())
|
||||
@ -65,7 +66,7 @@ Palette* load_pal_file(const char *filename)
|
||||
pal->addEntry(rgba(r, g, b, a));
|
||||
}
|
||||
|
||||
return pal.release();
|
||||
return pal;
|
||||
}
|
||||
|
||||
bool save_pal_file(const Palette *pal, const char *filename)
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Aseprite Document Library
|
||||
// Copyright (c) 2022 Igara Studio S.A.
|
||||
// Copyright (c) 2001-2015 David Capello
|
||||
//
|
||||
// This file is released under the terms of the MIT license.
|
||||
@ -8,13 +9,15 @@
|
||||
#define DOC_FILE_PAL_FILE_H_INCLUDED
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace doc {
|
||||
|
||||
class Palette;
|
||||
|
||||
namespace file {
|
||||
|
||||
Palette* load_pal_file(const char* filename);
|
||||
std::unique_ptr<Palette> load_pal_file(const char* filename);
|
||||
bool save_pal_file(const Palette* pal, const char* filename);
|
||||
|
||||
} // namespace file
|
||||
|
Loading…
x
Reference in New Issue
Block a user