From 946baad41a9771f5242dd1d5306b364769f5979d Mon Sep 17 00:00:00 2001 From: LordEidi Date: Mon, 12 Apr 2021 21:10:19 +0200 Subject: [PATCH] destructor added --- 32blit/graphics/surface.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/32blit/graphics/surface.cpp b/32blit/graphics/surface.cpp index 49b90aa71..cc8a0a307 100644 --- a/32blit/graphics/surface.cpp +++ b/32blit/graphics/surface.cpp @@ -51,6 +51,12 @@ namespace blit { Surface::Surface(uint8_t *data, const PixelFormat &format, const Size &bounds) : data(data), bounds(bounds), format(format) { init(); } + + Surface::~Surface() + { + if (_bNewData) delete[] data; + if (_bNewPalette) delete[] palette; + } /** * Loads a packed or raw image asset into a `Surface` @@ -641,6 +647,7 @@ namespace blit { if (format == PixelFormat::P || !is_raw) { // load palette ret->palette = new Pen[256]; + ret->_bNewPalette = true; file.read(offset, palette_entry_count * 4, (char *)ret->palette); offset += palette_entry_count * 4; } @@ -650,7 +657,10 @@ namespace blit { ret->data = (uint8_t *)file.get_ptr() + offset; else { if(!ret->data) - ret->data = new uint8_t[needed_size]; + { + ret->data = new uint8_t[needed_size]; + ret->_bNewData = true; + } file.read(offset, image.width * image.height * pixel_format_stride[image.format], (char *)ret->data); } @@ -658,7 +668,10 @@ namespace blit { } if(!ret->data) - ret->data = new uint8_t[needed_size]; + { + ret->data = new uint8_t[needed_size]; + ret->_bNewData = true; + } // avoid allocating if in flash const uint8_t *image_data, *end; @@ -755,6 +768,7 @@ namespace blit { // unpacked, no longer needed delete[] ret->palette; ret->palette = nullptr; + ret->_bNewPalette = false; } if (!file.get_ptr()) {