Image related leaks fix

This commit is contained in:
MihailRis 2023-11-19 17:44:14 +03:00
parent 6c70bb531b
commit cbafb9a68b
2 changed files with 9 additions and 7 deletions

View File

@ -2,9 +2,12 @@
#include "Assets.h"
#include <iostream>
#include <memory>
#include "../constants.h"
using std::unique_ptr;
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
}
@ -62,18 +65,17 @@ bool _load_texture(Assets* assets, const std::string& filename, const std::strin
}
bool _load_atlas(Assets* assets, const std::string& filename, const std::string& name) {
ImageData* image = png::load_image(filename);
unique_ptr<ImageData> image (png::load_image(filename));
if (image == nullptr) {
std::cerr << "failed to load image '" << name << "'" << std::endl;
return false;
}
for (int i = 0; i < ATLAS_MARGIN_SIZE; i++) {
ImageData* newimage = add_atlas_margins(image, 16);
delete image;
image = newimage;
ImageData* newimage = add_atlas_margins(image.get(), 16);
image.reset(newimage);
}
Texture* texture = Texture::from(image);
Texture* texture = Texture::from(image.get());
assets->store(texture, name);
return true;
}

View File

@ -326,12 +326,12 @@ ImageData* png::load_image(std::string filename) {
}
Texture* png::load_texture(std::string filename) {
ImageData* image = _png_load(filename.c_str());
unique_ptr<ImageData> image (_png_load(filename.c_str()));
if (image == nullptr){
std::cerr << "Could not load image " << filename << std::endl;
return nullptr;
}
return Texture::from(image);
return Texture::from(image.get());
}
void png::write_image(std::string filename, const ImageData* image) {