From 338c1b10a56db2b67e0341fb311f7bf3e09ac1c7 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 10 Mar 2026 21:49:47 +0100 Subject: [PATCH] Fix release handling --- contrib/vcpkg | 2 +- samples/main.cpp | 2 +- src/backends/sdl2_renderer.cpp | 8 ++++++++ src/backends/sdl2_renderer.h | 3 ++- src/tinyui.cpp | 14 +++----------- src/tinyui.h | 5 ----- src/widgets.cpp | 13 ++++++++++++- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/contrib/vcpkg b/contrib/vcpkg index c941d5e..f1fe3ac 160000 --- a/contrib/vcpkg +++ b/contrib/vcpkg @@ -1 +1 @@ -Subproject commit c941d5e450738629213a60ab8911fd26efca7c6e +Subproject commit f1fe3acb62b7aba476e48e3395c40d88478ac444 diff --git a/samples/main.cpp b/samples/main.cpp index faed220..3a7f42e 100644 --- a/samples/main.cpp +++ b/samples/main.cpp @@ -107,7 +107,7 @@ int main(int argc, char *argv[]) { Widgets::treeView(10, RootPanelId, "tree", Rect(100, 400, 100, 40)); Widgets::treeItem(11, 10, "Item 1"); - //Widgets::treeItem(12, 11, "Item 1"); + //Widgets::treeItem(12, 11, "Item 1.1"); Widgets::treeItem(13, 10, "Item 2"); Widgets::treeItem(14, 13, "Item 2.1"); diff --git a/src/backends/sdl2_renderer.cpp b/src/backends/sdl2_renderer.cpp index 6ee3ae2..49fc4cb 100644 --- a/src/backends/sdl2_renderer.cpp +++ b/src/backends/sdl2_renderer.cpp @@ -179,6 +179,7 @@ ret_code Renderer::releaseRenderer(Context &ctx) { ctx.mSDLContext.mDefaultFont = nullptr; } + IMG_Quit(); SDL_DestroyRenderer(ctx.mSDLContext.mRenderer); ctx.mSDLContext.mRenderer = nullptr; SDL_Quit(); @@ -485,4 +486,11 @@ SurfaceImpl *Renderer::createSurfaceImpl(unsigned char *data, int w, int h, int return surfaceImpl; } +void Renderer::releaseSurfaceImpl(SurfaceImpl *surfaceImpl) { + if (surfaceImpl != nullptr) { + surfaceImpl->clear(); + delete surfaceImpl; + } +} + } // namespace tinyui diff --git a/src/backends/sdl2_renderer.h b/src/backends/sdl2_renderer.h index 550ca68..c5870c5 100644 --- a/src/backends/sdl2_renderer.h +++ b/src/backends/sdl2_renderer.h @@ -38,7 +38,7 @@ struct SDL_Texture; namespace tinyui { struct SurfaceImpl { - SDL_Surface *mSurface; + SDL_Surface *mSurface{nullptr}; SurfaceImpl() = default; @@ -87,6 +87,7 @@ struct Renderer { static ret_code closeScreen(Context &ctx); static bool update(Context &ctx); static SurfaceImpl *createSurfaceImpl(unsigned char *data, int w, int h, int bytesPerPixel, int pitch); + static void releaseSurfaceImpl(SurfaceImpl *surfaceImpl); }; } // namespace tinyui diff --git a/src/tinyui.cpp b/src/tinyui.cpp index 5cc71de..b36c8c3 100644 --- a/src/tinyui.cpp +++ b/src/tinyui.cpp @@ -97,17 +97,6 @@ Context &TinyUi::getContext() { return *gCtx; } -ret_code TinyUi::init() { - auto &ctx = TinyUi::getContext(); - if (ctx.mCreated) { - return ErrorCode; - } - - ctx.mCreated = true; - - return ResultOk; -} - ret_code TinyUi::initScreen(int32_t x, int32_t y, int32_t w, int32_t h) { auto &ctx = TinyUi::getContext(); if (Renderer::initRenderer(ctx) == ErrorCode) { @@ -169,6 +158,9 @@ ret_code TinyUi::release() { } Renderer::releaseRenderer(ctx); Renderer::releaseScreen(ctx); + ctx.mSDLContext.mRenderer = nullptr; + ctx.mSDLContext.mWindow = nullptr; + ctx.mRoot = nullptr; ctx.mCreated = false; diff --git a/src/tinyui.h b/src/tinyui.h index 7c62af8..b0fed8d 100644 --- a/src/tinyui.h +++ b/src/tinyui.h @@ -414,11 +414,6 @@ struct TinyUi { /// @return The current tiny ui context. static Context &getContext(); - /// @brief Initialize the tiny ui. - /// @param ctx The context to initialize. - /// @return ResultOk if the initialization was successful, ErrorCode if not. - static ret_code init(); - /// @brief Initialize the screen. /// @param ctx The context to initialize. /// @param x The x-coordinate of the screen. diff --git a/src/widgets.cpp b/src/widgets.cpp index 6d67831..0c131df 100644 --- a/src/widgets.cpp +++ b/src/widgets.cpp @@ -74,12 +74,22 @@ static Image *loadIntoImageCache(Context &ctx, const char *filename) { image->mX = w; image->mY = h; image->mComp = bytesPerPixel; - ctx.mImageCache[filename] = image; return image; } +static void releaseImageCache(Context &ctx) { + for (auto it = ctx.mImageCache.begin(); it != ctx.mImageCache.end(); ++it) { + Image *image = it->second; + if (image != nullptr) { + Renderer::releaseSurfaceImpl(image->mSurfaceImpl); + delete image; + } + } + ctx.mImageCache.clear(); +} + static Widget *getValidRoot(Context &ctx) { if (ctx.mRoot != nullptr) { return ctx.mRoot; @@ -646,6 +656,7 @@ void Widgets::clear() { Widget *current{ctx.mRoot}; recursiveClear(current); + releaseImageCache(ctx); } bool Widgets::clearItem(Id id, bool recursive) {