Skip to content

Conversation

@bobtista
Copy link

@bobtista bobtista commented Oct 31, 2025

Replaces custom reference counting macros (MEMBER_ADD, MEMBER_RELEASE, SAFE_ADD_REF, SAFE_RELEASE_REF) with the type-safe RefCountPtr smart pointer from the core library.

Member Variables Migrated to RefCountPtr:

  • AssetInfoClass::m_pRenderObj
  • ScreenCursorClass::m_pTexture, m_pVertMaterial
  • PlaySoundDialogClass::SoundObj
  • SpherePropertySheetClass::m_RenderObj
  • RingPropertySheetClass::m_RenderObj
  • CW3DViewDoc::m_pCRenderObj, m_pCAnimation, m_pCAnimCombo, m_pCursor, m_pCursorScene
  • CGraphicView::m_pCamera, m_pLightMesh

Local Variable Usage:

  • Replaced MEMBER_ADD/MEMBER_RELEASE with REF_PTR_SET/REF_PTR_RELEASE for local raw pointers in 15 files

Removed Macros (Utils.h):

  • MEMBER_ADD
  • MEMBER_RELEASE
  • SAFE_ADD_REF
  • SAFE_RELEASE_REF

Notes:

  • Used Create_AddRef() when taking ownership of an existing pointer
  • Used Create_NoAddRef() when wrapping factory methods that return ref=1
  • Added .Peek() calls when passing to functions expecting raw pointers
  • Updated getter methods to return .Peek() instead of raw RefCountPtr
  • Preserved all original logic and formatting

@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch 8 times, most recently from cc1d8dc to c3f8d36 Compare October 31, 2025 02:22
@Skyaero42 Skyaero42 added Refactor Edits the code with insignificant behavior changes, is never user facing Stability Concerns stability of the runtime labels Nov 1, 2025
Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I continue review, please make another pass on this and correct all missing resource cleanups. It is important that no logic is changed, unless it fixes a bug.


// Was there a valid display object?
if (m_pCRenderObj)
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition above is obsoleted.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, yes the operations inside are safe to execute regardless. If I understand correctly, the conditional checks make the intent explicit (only cleanup when there's something to clean up). RefCountPtr provides operator const DummyPtrType *() specifically to enable null-checking in boolean contexts.
// From ref_ptr.h documentation (lines 92-96):
void Do_It(void)
{
if (MyObject) { // Recommended pattern
MyObject->Do_Something();
}
}

This is also the pattern we use elsewhere eg CW3DViewDoc::CleanupResources.

Happy to keep or remove them, up to you

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment functions already do the tests and are safe.

const RefCountPtr<T> & operator =(DummyPtrType * dummy_ptr)
{
if (Referent) {
Referent->Release_Ref();
}
Referent = 0;
return *this;
}

Alternatively, use Clear() function. Maybe that is cleaner.

void Clear(void)
{
if (Referent) {
Referent->Release_Ref();
Referent = 0;
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like Clear :) Done

@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch from c3f8d36 to 0cbd2fc Compare November 2, 2025 18:27
…o RefCountPtr<T>

Replace MEMBER_ADD/MEMBER_RELEASE macros with RefCountPtr<T> smart pointers.

- AssetInfo: m_pRenderObj now RefCountPtr<RenderObjClass>
- ScreenCursor: m_pTexture and m_pVertMaterial now RefCountPtr
- PlaySoundDialog: SoundObj now RefCountPtr<AudibleSoundClass>
Replace MEMBER_ADD/MEMBER_RELEASE macros with RefCountPtr<T> smart pointers.

- SpherePropertySheet: m_RenderObj now RefCountPtr<SphereRenderObjClass>
- RingPropertySheet: m_RenderObj now RefCountPtr<RingRenderObjClass>
- EmitterPropertySheet: local emitter variables use REF_PTR_RELEASE
- EmitterInstanceList: use REF_PTR_RELEASE for vector cleanup
Replace MEMBER_ADD/MEMBER_RELEASE macros with RefCountPtr<T> and REF_PTR_RELEASE.

- W3DViewDoc: m_pCRenderObj, m_pCAnimation, m_pCursorScene, m_pCursor now RefCountPtr
- GraphicView: m_pCamera and m_pLightMesh now RefCountPtr
- Local variables in Utils, DataTreeView, BoneMgrDialog, etc. use REF_PTR_RELEASE
Remove obsolete macros now replaced by RefCountPtr<T> and core library macros.

- Removed: MEMBER_ADD, MEMBER_RELEASE, SAFE_ADD_REF, SAFE_RELEASE_REF
- Kept: SAFE_DELETE, SAFE_DELETE_ARRAY, COM_RELEASE, SAFE_CLOSE, SANITY_CHECK
@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch from 0cbd2fc to 4c2acf0 Compare November 2, 2025 18:28
Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we first make a change where we just remove the duplicate Macros, without adding RefCountPtr? Then this change will be simpler? And then make small changes to introduce RefCountPtr slowly? I am afraid this will be a big review here otherwise given the implications. One mistake with ref counting and the game will crash.

m_pCursor->Remove ();
}
MEMBER_RELEASE (m_pCursorScene);
m_pCursor.Clear();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clear is wrong.

// Remove this object from the scene
Remove_Object_From_Scene (m_pCRenderObj.Peek());
}
m_pCRenderObj.Clear();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be moved into scope above.

m_pCRenderObj = NULL;
Remove_Object_From_Scene (m_pCRenderObj.Peek());
}
m_pCRenderObj.Clear();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be moved into scope above

{
// Lose the animation
SAFE_DELETE (m_pCAnimCombo);
MEMBER_RELEASE (m_pCAnimation);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong.

MEMBER_RELEASE (m_pCAnimation);
m_pCAnimation = WW3DAssetManager::Get_Instance()->Get_HAnim (pszAnimationName);
m_pCAnimation = RefCountPtr<HAnimClass>::Create_NoAddRef(WW3DAssetManager::Get_Instance()->Get_HAnim (pszAnimationName));
ASSERT (m_pCAnimation);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.Peek()

Multiple times in this change. Please check what's up with ASSERT.

@bobtista
Copy link
Author

bobtista commented Nov 3, 2025

How about we first make a change where we just remove the duplicate Macros, without adding RefCountPtr? Then this change will be simpler? And then make small changes to introduce RefCountPtr slowly? I am afraid this will be a big review here otherwise given the implications. One mistake with ref counting and the game will crash.

Ok, check out 1784. Shall we continue this PR after rebasing and have it focus just on just W3DView, then additional RefCountPtr migrations can have separate PRs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Refactor Edits the code with insignificant behavior changes, is never user facing Stability Concerns stability of the runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants