Greetings! )
I’m trying to understand how to work with SharedPtr correctly and stumbled upon an indefinite behavior that put me in a dead end. It is expected that after deleting an object, the number of links will decrease, but this does not happen.
Urho3D::SharedPtr<Urho3D::Node> m_rootNode;
...
#define DELETE_URHO_PTR(_name,_method) \
LOG_DBG(#_name " isnull? " << _name.Null()) \
if(_name.NotNull()) \
{ \
LOG_DBG(#_name " before refs=" << _name.Refs()) \
_method; \
delete _name; \
LOG_DBG(#_name " after refs=" << _name.Refs()) \
}
#define DELETE_URHO_REMOVABLE(_name) DELETE_URHO_PTR(_name, _name->Remove())
...
DELETE_URHO_REMOVABLE(m_rootNode);
Logs:
[2018-12-09 13:53:37] {140326202556608} [Debg] m_rootNode isnull? false
[2018-12-09 13:53:37] {140326202556608} [Debg] m_rootNode before refs=3
[2018-12-09 13:53:37] {140326202556608} [Debg] m_rootNode after refs=-1411174928
I also often use the “create on access” technique. Do I understand correctly that the .Get() method will not change the number of refs count?
Urho3D::Node *CWorldObject::rootNode()
{
if(m_rootNode.Null())
{
m_rootNode = ST->graphic()->scene()->CreateChild();
}
return m_rootNode.Get();
}
...
Urho3D::RigidBody *CWorldObject::body()
{
if(m_body.Null())
{
m_body = rootNode()->CreateComponent<Urho3D::RigidBody>();
m_body->SetCollisionEventMode(Urho3D::COLLISION_ALWAYS);
}
return m_body.Get();
}
In general, I would very much like not to use SharedPtr in my project, but to work with objects directly. But as I understand it, CreateChild/CreateComponent, in addition to constructing objects, also performs various bindings that are not performed in object constructors, that is, the part of object initialization is taken out of an object …
How safe will it be to not use SharedPtr/CreateComponent?