-
Notifications
You must be signed in to change notification settings - Fork 146
refactor(w3dview): Migrate from MEMBER_ADD/MEMBER_RELEASE macros to RefCountPtr<T> #1763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
refactor(w3dview): Migrate from MEMBER_ADD/MEMBER_RELEASE macros to RefCountPtr<T> #1763
Conversation
cc1d8dc to
c3f8d36
Compare
xezon
left a comment
There was a problem hiding this 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.
Core/Tools/W3DView/W3DViewDoc.cpp
Outdated
|
|
||
| // Was there a valid display object? | ||
| if (m_pCRenderObj) | ||
| { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
GeneralsGameCode/Core/Libraries/Source/WWVegas/WWLib/ref_ptr.h
Lines 289 to 298 in 7a6402b
| const RefCountPtr<T> & operator =(DummyPtrType * dummy_ptr) | |
| { | |
| if (Referent) { | |
| Referent->Release_Ref(); | |
| } | |
| Referent = 0; | |
| return *this; | |
| } |
Alternatively, use Clear() function. Maybe that is cleaner.
GeneralsGameCode/Core/Libraries/Source/WWVegas/WWLib/ref_ptr.h
Lines 352 to 358 in 7a6402b
| void Clear(void) | |
| { | |
| if (Referent) { | |
| Referent->Release_Ref(); | |
| Referent = 0; | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like Clear :) Done
0cbd2fc to
4c2acf0
Compare
xezon
left a comment
There was a problem hiding this 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.
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? |
3c0d862 to
d3cd83c
Compare
|
This still needs work. |
1229310 to
52e834f
Compare
xezon
left a comment
There was a problem hiding this 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 (); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_pCBackObjectScene.Clear() missing?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
81a71ad to
bad267f
Compare
Core/Tools/W3DView/W3DViewDoc.cpp
Outdated
| SAFE_DELETE (m_pCAnimCombo); | ||
| REF_PTR_RELEASE (m_pCAnimation); | ||
|
|
||
| if (m_pCRenderObj != NULL) { |
There was a problem hiding this comment.
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
|
I think this is ready for another look |
bad267f to
e2d2ae2
Compare
| DisplayObject (pCModel); | ||
|
|
||
| // Get an instance of the animation object | ||
| SAFE_DELETE (m_pCAnimCombo); |
There was a problem hiding this comment.
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
…rScene in CleanupResources
…s to RefCountPtr idiomatic form
e2d2ae2 to
9bdab84
Compare
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:
Local Variable Usage:
Removed Macros (Utils.h):
Notes: