Skip to content

Commit 1b7e9e8

Browse files
authored
Fix invalid references counter to TXD after engineSetModelTXDID (#4043)
1 parent a3b7c85 commit 1b7e9e8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Client/game_sa/CModelInfoSA.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,16 +801,28 @@ void CModelInfoSA::SetTextureDictionaryID(unsigned short usID)
801801
if (!m_pInterface)
802802
return;
803803

804-
// Remove ref from the old TXD
805-
CTxdStore_RemoveRef(m_pInterface->usTextureDictionary);
804+
// CBaseModelInfo::AddRef adds references to model and TXD
805+
// We need transfer added references from old TXD to new TXD
806+
size_t referencesCount = m_pInterface->usNumberOfRefs;
807+
808+
// +1 reference for active rwObject
809+
// The current textures will be removed in RpAtomicDestroy
810+
// RenderWare uses an additional reference counter per texture
811+
if (m_pInterface->pRwObject)
812+
referencesCount++;
813+
814+
for (size_t i = 0; i < referencesCount; i++)
815+
CTxdStore_RemoveRef(m_pInterface->usTextureDictionary);
806816

807817
// Store vanilla TXD ID
808818
if (!MapContains(ms_DefaultTxdIDMap, m_dwModelID))
809819
ms_DefaultTxdIDMap[m_dwModelID] = m_pInterface->usTextureDictionary;
810820

811821
// Set new TXD and increase ref of it
812822
m_pInterface->usTextureDictionary = usID;
813-
CTxdStore_AddRef(usID);
823+
824+
for (size_t i = 0; i < referencesCount; i++)
825+
CTxdStore_AddRef(usID);
814826
}
815827

816828
void CModelInfoSA::ResetTextureDictionaryID()

Client/game_sa/CRenderWareSA.TextureReplacing.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ void CRenderWareSA::ModelInfoTXDRemoveTextures(SReplacementTextures* pReplacemen
266266
ListRemove(currentTextures, pOriginalTexture);
267267
}
268268
assert(currentTextures.empty());
269-
#endif
270269

270+
int32_t refsCount = CTxdStore_GetNumRefs(pInfo->usTxdId);
271+
assert(refsCount > 0, "Should have at least one TXD reference here");
272+
#endif
271273
// Remove info
272274
CTxdStore_RemoveRef(pInfo->usTxdId);
273275
MapRemove(ms_ModelTexturesInfoMap, usTxdId);

0 commit comments

Comments
 (0)