From 7244534ac478adcdb76ce4c9d2d6c7bbe77f0761 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Fri, 17 Jul 2026 15:18:06 +0800 Subject: [PATCH] fix: opengl resource delete error --- include/Cubed/debug_collector.hpp | 3 +++ include/Cubed/tools/font.hpp | 3 +++ src/app.cpp | 8 +++++++- src/debug_collector.cpp | 9 ++++++--- src/tools/font.cpp | 10 ++++++++-- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/include/Cubed/debug_collector.hpp b/include/Cubed/debug_collector.hpp index 4de3f8a..5de74fd 100644 --- a/include/Cubed/debug_collector.hpp +++ b/include/Cubed/debug_collector.hpp @@ -5,6 +5,7 @@ #include "Cubed/ui/widget.hpp" #include +#include #include namespace Cubed { @@ -12,6 +13,7 @@ namespace Cubed { class DebugCollector { public: static DebugCollector& get(); + static void distory(); DebugCollector(); void report(const std::string& name, std::string_view content); @@ -22,6 +24,7 @@ public: private: ColumnLayout m_widget; std::unordered_map m_component; + static std::unique_ptr& get_ptr(); }; template diff --git a/include/Cubed/tools/font.hpp b/include/Cubed/tools/font.hpp index 6aad63e..55d6d7f 100644 --- a/include/Cubed/tools/font.hpp +++ b/include/Cubed/tools/font.hpp @@ -51,6 +51,8 @@ public: Font(); ~Font(); static Font& get(); + + static void destroy(); TextMesh vertices(const std::string& text); const Texture* text_texture(); static const std::string& font_path(); @@ -70,6 +72,7 @@ private: int m_max_layers = 0; Glyph& load_glyph(uint32_t glyph_index); void upload_glyph(Glyph& glyph, const unsigned char* buffer); + static std::unique_ptr& get_ptr(); }; } // namespace Cubed \ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index 9f2fb06..e67f25f 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -6,6 +6,7 @@ #include "Cubed/localization.hpp" #include "Cubed/tools/arg_parser.hpp" #include "Cubed/tools/cubed_assert.hpp" +#include "Cubed/tools/font.hpp" #include "Cubed/tools/log.hpp" #include "Cubed/tools/system_info.hpp" #include "Cubed/tools/system_locate.hpp" @@ -13,6 +14,7 @@ #include #include + namespace Cubed { App::App() @@ -21,7 +23,11 @@ App::App() m_texture_manager(m_game_config), m_audio(m_game_config), m_renderer(m_texture_manager, m_game_config), m_scene_manager(*this) {} -App::~App() { stop_text_input(); } +App::~App() { + stop_text_input(); + Font::destroy(); + DebugCollector::distory(); +} void App::init(int argc, char** argv) { handle_argument(argc, argv); diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp index 0a86abd..ef251d6 100644 --- a/src/debug_collector.cpp +++ b/src/debug_collector.cpp @@ -4,16 +4,19 @@ #include "version.hpp" #include +#include namespace Cubed { DebugCollector::DebugCollector() : m_widget(nullptr) {} -DebugCollector& DebugCollector::get() { - static DebugCollector instance; +DebugCollector& DebugCollector::get() { return *get_ptr(); } +std::unique_ptr& DebugCollector::get_ptr() { + static std::unique_ptr instance = + std::make_unique(); return instance; } - +void DebugCollector::distory() { get_ptr().reset(); } void DebugCollector::init(int width, int height) { constexpr float SCALE = 0.6f; diff --git a/src/tools/font.cpp b/src/tools/font.cpp index 1bd3c27..fd986a2 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -2,6 +2,9 @@ #include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/log.hpp" + +#include + namespace fs = std::filesystem; namespace Cubed { @@ -72,10 +75,13 @@ void Font::upload_glyph(Glyph& glyph, const unsigned char* buffer) { glyph.size.y / float(CELL_SIZE)}; } -Font& Font::get() { - static Font font; +Font& Font::get() { return *get_ptr(); } +std::unique_ptr& Font::get_ptr() { + static std::unique_ptr font = std::make_unique(); return font; } +void Font::destroy() { get_ptr().reset(); } + TextMesh Font::vertices(const std::string& text) { hb_buffer_t* buffer = hb_buffer_create();