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 2 times, most recently 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.

@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?

@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch from 3c0d862 to d3cd83c Compare November 6, 2025 17:18
@xezon
Copy link

xezon commented Nov 22, 2025

This still needs work.

@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch from 1229310 to 52e834f Compare November 22, 2025 18:17
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.

This refactor needs another pass. Something went wrong the indentation of code. And behavior has changed.

if (m_pCBackObjectScene)
{
// Free the scene object
m_pCBackObjectScene->Release_Ref ();
Copy link

@xezon xezon Nov 22, 2025

Choose a reason for hiding this comment

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

m_pCBackObjectScene.Clear() missing?

Copy link
Author

Choose a reason for hiding this comment

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

m_pCBackObjectScene is a raw pointer (SceneClass *), not a RefCountPtr, so it uses Release_Ref() and = NULL for cleanup (lines 167-168). Want me to convert it to RefCountPtr? Or should that be in a separate PR?

Copy link
Author

Choose a reason for hiding this comment

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

Checking again - On main:
m_pCursorScene used REF_PTR_RELEASE and was migrated to RefCountPtr with .Clear().
m_pCBackObjectScene used manual Release_Ref() + = NULL (not REF_PTR_RELEASE), so it wasn’t done in this PR. Do you want me to migrate it in this PR?

@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch 4 times, most recently from 81a71ad to bad267f Compare December 3, 2025 16:23
SAFE_DELETE (m_pCAnimCombo);
REF_PTR_RELEASE (m_pCAnimation);

if (m_pCRenderObj != NULL) {
Copy link
Author

Choose a reason for hiding this comment

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

This indentation seems wrong in the original code, fixed here

@bobtista
Copy link
Author

bobtista commented Dec 3, 2025

I think this is ready for another look

@bobtista bobtista force-pushed the bobtista/refactor-w3dview-refcountptr branch from bad267f to e2d2ae2 Compare December 15, 2025 20:17
DisplayObject (pCModel);

// Get an instance of the animation object
SAFE_DELETE (m_pCAnimCombo);
Copy link
Author

Choose a reason for hiding this comment

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

Here too, the indentation was weird in the original. It looks like they meant to do:
Function body level: 4 spaces
Inside the if block: 8 spaces (4 + 4)
But a few spots got 10 spaces, eg SAFE_DELETE, so I fixed those, at least here

…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 e2d2ae2 to 9bdab84 Compare January 14, 2026 21:32
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