ActorNode/PhysicalNode memory leak?

I am having a problem with ActorNode (actually PhysicalNode) not being destructed i.e. leaking memory. I am using version 1.7.2 that I have built myself.
The following code shows the problem

void test()
{
	PT(ActorNode) p = new ActorNode("P");
	cout << "ref count = " << p->get_ref_count() << "\n";
}

When (sometime after) p goes out of scope the destructor for it should be called. It is never called and so will leak memory.
To see this add cout to the ActorNode destructor and recompile the Panda3D source or create a new class derived from ActorNode (which is what I want to do) and use that in the above example. The symptoms are that the reference count is 2 and not 1 which is what I would expect.
If you remove the physical node

void test()
{
	PT(ActorNode) p = new ActorNode("P");
	cout << "ref count = " << p->get_ref_count() << "\n";
	p->remove_physical(0);
	cout << "ref count = " << p->get_ref_count() << "\n";
}

you will see that the reference count goes from 2 to 1 and then the destructor will be called after p goes out of scope.
The problem seems to be in PhysicalNode::add_physical which is called by the ActorNode constructor

INLINE void PhysicalNode::
add_physical(Physical *physical) {
	 _physicals.push_back(physical);
	physical->_physical_node = this;
	physical->_physical_node_path = NodePath(this);
}

Note the creation of the NodePath(this) and that _physical_node_path will continue to hold a reference to this.
So, it this the expected behaviour i.e. that ActorNodes will never be destroyed, is it a bug, or am I just missing something? Anyone else have the same problem?

Thanks for pointing this out. You’re right, this is a circular reference, and this needs to be resolved.

It doesn’t seem to me like get_physical_node_path is used a lot, so I’m sorely tempted to remove it. But I don’t want to break anything, so I think I’ll just redefine that method to return a brand new NodePath every time.

Fix committed for 1.8.1. Thanks again, and welcome to the community. :slight_smile: