Skip to content

Commit 3308359

Browse files
authored
Fix memory leak in engineImageLinkDFF (#4544)
1 parent 2e2b4a3 commit 3308359

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Client/game_sa/CStreamingSA.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CModelInfoSA.h"
1616
#include "Fileapi.h"
1717
#include "processthreadsapi.h"
18+
#include "CGameSA.h"
1819

1920
extern CCoreInterface* g_pCore;
2021

@@ -334,6 +335,14 @@ void CStreamingSA::SetStreamingInfo(uint modelid, unsigned char usStreamID, uint
334335
{
335336
CStreamingInfo* pItemInfo = GetStreamingInfo(modelid);
336337

338+
// We remove the existing RwObject because, after switching the archive, the streamer will load a new one.
339+
// ReInit doesn't delete all RwObjects unless certain conditions are met.
340+
// In this case, we must force-remove the RwObject from memory, because it is no longer used,
341+
// and due to the archive change the streamer no longer detects it and therefore won't delete it.
342+
// As a result, a memory leak occurs after every call to engineImageLinkDFF.
343+
if (CModelInfo* modelInfo = g_pCore->GetGame()->GetModelInfo(modelid); modelInfo->GetRwObject())
344+
RemoveModel(modelid);
345+
337346
// Change nextInImg field for prev model
338347
for (CStreamingInfo& info : ms_aInfoForModel)
339348
{

Client/mods/deathmatch/logic/CClientIMG.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ bool CClientIMG::LinkModel(unsigned int uiModelID, size_t uiFileID)
211211

212212
m_restoreInfo.emplace_back(uiModelID, pCurrInfo->offsetInBlocks, pCurrInfo->sizeInBlocks, pCurrInfo->archiveId);
213213

214+
// Internally stream out the vehicle before calling CStreamingSA::RemoveModel
215+
// otherwise a crash will occur if the player is inside a vehicle that gets unloaded by the streamer
216+
if (CClientVehicleManager::IsValidModel(uiModelID))
217+
g_pClientGame->GetVehicleManager()->RestreamVehicles(static_cast<unsigned short>(uiModelID));
218+
214219
g_pGame->GetStreaming()->SetStreamingInfo(uiModelID, m_ucArchiveID, pFileInfo->uiOffset, pFileInfo->usSize);
215220

216221
return true;

0 commit comments

Comments
 (0)