From 07c9f9959d3ce46da8bc7b0777d803524f9d1ec0 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 5 Oct 2022 12:39:29 +0200 Subject: [PATCH] Util: Fix singleton resource cleanup --- src/ruc/singleton.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ruc/singleton.h b/src/ruc/singleton.h index ee12728..a550b4d 100644 --- a/src/ruc/singleton.h +++ b/src/ruc/singleton.h @@ -6,7 +6,7 @@ #pragma once -#include // malloc +#include // free, malloc #include "meta/assert.h" @@ -20,7 +20,7 @@ public: if (s_instance == nullptr) { // Allocate memory for this instance s_instance = static_cast(malloc(sizeof(T))); - // Call the constructor using placement new + // Call the constructor using "placement new" new (s_instance) T { s {} }; } @@ -33,7 +33,11 @@ public: return; } - delete s_instance; + // Call the destructor + s_instance->~T(); + // Deallocate memory + free(static_cast(s_instance)); + s_instance = nullptr; }