Archive 17/01/2023.

Stack allocated Urho3D::RefCounted variable

SeeSoftware

Do all RefCounted objects have to be allocated on the heap like this:

SomeObject *foo = new SomeObject(context_);
foo->ReleaseRef(); // <- safe

or can you have them on the stack too:

SomeObject bar(context_)
bar.ReleaseRef(); // <- unsafe ?
magic.lixin

it`s OK I guess, only refCount_ is allocated in heap

Eugene

It’s okay to create Objects on the stack if you could gurantee that neither you nor Urho internal routines create SharedPtr-s for this object.

SirNate0

I think that you could even get away with creating SharedPtrs as long as you manually incremented the reference count first and the SharedPtrs are released before the object goes out of scope, though I’m not certain of this…