Cleaning up references

I’m quite confused about which Panda references I should clean up in my C++ pgm.

For example, if I do this:

NodePath carBody = window->load_model(framework.get_models(), "models/car1/car_box");
carBody.reparent_to(window->get_render());

What is needed here to clean up this reference… anything?

Another example:

PT(GenericAsyncTask) carTaskObject = new GenericAsyncTask("ode car task", &ODECar::carTask, (void*)this);
taskMgr->add(carTaskObject);
...
carTaskObject->remove();

I have gotten the impression that the remove() is necessary here, right? Anything else that must be done?

I’m also looking for if there are any general guidelines of how to clean up the Panda references (when quitting the pgm)?

In general, NodePaths that have been connected to the scene graph (for instance, by reparenting them to render) must be explicitly removed to disconnect them, or they will remain connected and visible:

carBody.remove_node();

Calling carTaskObject->remove() is one way to stop a task when you’re done with it. Another way to achieve the same thing is to return DS_done from your task function; if you do this then you don’t need to call remove() explicitly.

Calling framework.close_framework() should be all you have to do to clean up Panda when quitting the program. Or just set _exit_flag to true, so that main_loop() will exit. This won’t necessarily free all references you’ve allocated, but since the program is quitting that generally doesn’t matter. close_framework() will make sure that the resources outside of your process (like graphics contexts and audio handles) are correctly released.

David

Thanks for the quick reply.
It is when quitting the pgm I want to make sure that everything is cleaned up correctly, so it’s good to hear that close_framework() does the job. :slight_smile:

Just to be sure…
If I create following Panda objects with these lines:

PT(AmbientLight) alight = new AmbientLight("alight");
...
PT(GenericAsyncTask) carTaskObject = new GenericAsyncTask("ode car task", &(ODECar::carTask), (void*)this);

When quitting my pgm, must I delete these references explicitly (via the delete keyword)?

No. In fact, you should never use the delete keyword on any ReferenceCount-derived object. Particularly not on pointers that you store in a PT(Something) type pointer.

This is because the ReferenceCount system (which is automatically invoked by the PT(Something) pointer) will be responsible for deleting the object when the last reference goes away. If you explicitly call delete on it yourself, you will cause a double-delete, which will be bad.

David

OK. Thanks for clarifying that. :slight_smile: