From d0ec49cc5269f3a73e41478770e8cabdef56b633 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Fri, 8 Nov 2024 21:51:04 +0100 Subject: [PATCH] RTech: fix resource leak CustomPakData_t::numHandles was never decremented on unload, causing pakId == PAK_INVALID_HANDLE to be true and triggering the assert. Due to this, we either never unload resources properly on subsequent unloads and loads, or we would run out of pak slots as numHandles will reach MAX_CUSTOM_PAKS. Also added a comment explaining why this loop isn't ran in reverse. --- src/engine/cmodel_bsp.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/engine/cmodel_bsp.cpp b/src/engine/cmodel_bsp.cpp index 36f874a3..d831769f 100644 --- a/src/engine/cmodel_bsp.cpp +++ b/src/engine/cmodel_bsp.cpp @@ -61,19 +61,17 @@ void CustomPakData_t::UnloadAndRemoveAll() { // Base SDK paks should not be unloaded here, but only right before base // engine paks are unloaded. Only unload user requested and level settings - // paks from here. - for (size_t i = CustomPakData_t::PAK_TYPE_COUNT; i < numHandles; i++) + // paks from here. Also, ideally this loop runs in reverse, but the engine + // does not support that as it would crash when paks are unloaded that way. + for (size_t i = CustomPakData_t::PAK_TYPE_COUNT, n = numHandles; i < n; i++) { const PakHandle_t pakId = handles[i]; - - if (pakId == PAK_INVALID_HANDLE) - { - assert(0); // invalid handles should not be inserted - return; - } + assert(pakId != PAK_INVALID_HANDLE); // invalid handles should not be inserted g_pakLoadApi->UnloadAsync(pakId); handles[i] = PAK_INVALID_HANDLE; + + numHandles--; } }