Skip to content

Commit

Permalink
Guard Morph Mesh Visual object from destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiyansKing committed Oct 1, 2021
1 parent fe3caeb commit 19c6c27
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
8 changes: 5 additions & 3 deletions D3D11Engine/GothicAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,8 +1546,10 @@ void GothicAPI::OnAddVob( zCVob* vob, zCWorld* world ) {

// Load the new visual
MeshVisualInfo* mi = new MeshVisualInfo;
if ( ext == ".MMS" )
if ( ext == ".MMS" ) {
mi->MorphMeshVisual = (void*)vob->GetVisual();
zCObject_AddRef( mi->MorphMeshVisual );
}

WorldConverter::Extract3DSMeshFromVisual2( pm, mi );
StaticMeshVisuals[pm] = mi;
Expand Down Expand Up @@ -1971,7 +1973,7 @@ void GothicAPI::DrawParticleFX( zCVob* source, zCParticleFX* fx, ParticleFrameDa
kill = pfx;
if ( kill && (kill->LifeSpan < *fx->GetPrivateTotalTime()) ) {
if ( kill->PolyStrip )
kill->PolyStrip->Release(); // TODO: MEMLEAK RIGHT HERE!
zCObject_Release( kill->PolyStrip ); // TODO: MEMLEAK RIGHT HERE!

pfx = kill->Next;
fx->SetFirstParticle( pfx );
Expand All @@ -1989,7 +1991,7 @@ void GothicAPI::DrawParticleFX( zCVob* source, zCParticleFX* fx, ParticleFrameDa
kill = p->Next;
if ( kill && (kill->LifeSpan < *fx->GetPrivateTotalTime()) ) {
if ( kill->PolyStrip )
kill->PolyStrip->Release();
zCObject_Release( kill->PolyStrip );

p->Next = kill->Next;
kill->Next = *(zTParticle**)GothicMemoryLocations::GlobalObjects::s_globFreePart;
Expand Down
3 changes: 3 additions & 0 deletions D3D11Engine/WorldObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ struct MeshVisualInfo : public BaseVisualInfo {
}

~MeshVisualInfo() {
if ( MorphMeshVisual ) {
zCObject_Release( MorphMeshVisual );
}
delete FullMesh;
}

Expand Down
11 changes: 11 additions & 0 deletions D3D11Engine/pch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

#include <d3d9.h>

void zCObject_AddRef( void* o ) {
DWORD object = reinterpret_cast<DWORD>( o );
++( *reinterpret_cast<DWORD*>( object + 0x04 ) );
}

void zCObject_Release( void* o ) {
DWORD object = reinterpret_cast<DWORD>( o );
if ( --( *reinterpret_cast<DWORD*>( object + 0x04 ) ) <= 0 )
reinterpret_cast<void( __thiscall* )( DWORD, DWORD )>( *reinterpret_cast<DWORD*>( *reinterpret_cast<DWORD*>( object ) + 0x0C ) )( object, 1 );
}

void DebugWrite_i( LPCSTR lpDebugMessage, void* thisptr ) {
//LogInfo() << "D3D7-CALL (" << thisptr << "): " << lpDebugMessage;

Expand Down
4 changes: 4 additions & 0 deletions D3D11Engine/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ extern bool GMPModeActive;
#define SAFE_DELETE(x) delete x; x = nullptr;
//#define V(x) x

/** zCObject Managing */
void zCObject_AddRef( void* o );
void zCObject_Release( void* o );

/** Writes a string of the D3D7-Call log */
void DebugWrite_i( LPCSTR lpDebugMessage, void* thisptr );

Expand Down
9 changes: 0 additions & 9 deletions D3D11Engine/zCObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

class zCObject {
public:
void Release() {
refCtr--;
int temp = refCtr;
if ( temp <= 0 ) {
// Call destructor
delete this;
}
}

// Recreate V-Table
virtual zCClassDef* _GetClassDef() = 0;
virtual void Archive() = 0;
Expand Down

0 comments on commit 19c6c27

Please sign in to comment.