File tree Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Original file line number Diff line number Diff 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
816828void CModelInfoSA::ResetTextureDictionaryID ()
Original file line number Diff line number Diff 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);
You can’t perform that action at this time.
0 commit comments