Can I build a self-destruct Class ?

Hi, I’ve been trying to destroy a class instanced from the DirectObject. I can do this from the upper class by calling :


myNewClass.__del__()

Is it possible to destroy the class from within itself, or I’m lacking of OOP concepts ?

Thanks.
JO

You mean a call of self.del() within the class? I think python would allow such a call, but the actual reference to the object would not truly be deleted. The object is the one making the call, so a reference would need to be kept to do so.

Right. In Python, everything is implicitly reference-counted. There’s no way to delete an object until all of its references have gone out of scope, and then it is deleted automatically.

Newcomers to Python, especially people used to C++ (which does not have this implicit reference counting), often come across the “del” command of Python, which looks like this:


del myObject

And they confuse this with the C++ “delete” command, and use it all the time, believing that it will destroy myObject. In fact, it does nothing of the sort; all it does is remove the variable myObject from the current scope. The actual object itself may or may not subsequently be destroyed, according to whether there are any other references to it. (There are not very many cases in which you need to use the del command.)

You should never attempt to call the del method explicitly. Python will call it when it the object is being deleted. Normally, you shouldn’t need to think about deleting an object explicitly; just forget about the object, and let Python delete it when it’s safe to.

David

Some additional food for thought:

When a DirectObject accepts a message (via self.accept), Panda’s messenger retains a reference to the object. So if you eliminate all other references but leave the object accepting messages, the object won’t be deleted. I recently added some language to the Panda manual to point this out.

The solution used in the DirectGui framework is to equip every object with a destroy() command. Destroy can be explicitly called, and while it doesn’t get rid of the object, it does ignore the objects messages (as well as do other housekeeping to get the object out of the way). This paradigm might be close to what you want if you’ve created objects that manage their own message-listening.

Take care,
Mark

Thanks for the input. However, when I call del(), all events that have been queried by self.accept were no longer recieved (or maybe ignored), I’ve debugged that thing. It’s for my custom GUI dialogs and buttons. After a decision from user have been achieved, I removed the dialog & buttons’ cards & geoms, and the collision node. By doing so, the collision traverser will no longer has anything to traverse, so it (and the collision handler) will be terminated automatically ? So, if I don’t need to call del from outside, then from inside the class, the next thing I need to do is calling self.ignoreAll() to stop recieving events?

Thanks a lot.

You mention “GUI dialogs and buttons”. Are these DirectGui objects? When you are done with a DirectGui object, as Fixer points out, you should call destroy() on it. This will automatically remove it from the screen and call ignore() for all message hooks added by the DirectGui system.

I’m not sure what you’re talking about in relation to collisions. A DirectGui object will not itself be involved in collisions. If you’re referring to some other object in the scene graph, then you need to remove it from the collision handler if you ever added it. That is, if it is a “from” object, then you do need to explicitly remove it from the collision handler. If it is an “into” object, which is just a CollisionNode in the scene graph that you never added to the collision handler, then you don’t need to do anything special to clean it up.

If you ever added any messenger hooks for your object, via object.accept() or object.acceptOnce(), you need to remove those via object.ignore() or object.ignoreAll().

If you started any tasks running that involve your object, you need to stop those.

If you started any intervals playing that involve your object, you need to stop those (unless you don’t mind letting the intervals continue playing to completion after you have “deleted” your object).

If the object represents a node, and you attached it into a scene graph, you should detach it using nodePath.detach() or nodePath.removeNode().

In short, anything you did, you need to undo. But still, there’s no way to explicitly “delete” an object in Python. You just stop using it.

David

We could just say there is a few way to say ’ I do not use anymore" :slight_smile:

We could just say there is a few way to say ’ I do not use anymore" :slight_smile:

No, my dialogs & buttons are not DirectGUI objects, I created them primitively using Cardmaker and set the geoms of buttons for collision with the collision ray, just to give more flexibility and visual sync to the HUD and navigational purposes.

Thanks for everything.
JO