Maya API c++ How do ~destructor work?

Hey guys,
I am trying to learn C++ and Maya Api all at once and I got a problem in relation to the destruction of a Node google can’t answer clearly for me.
And to make it worst, it is not my code and I need to figure out where the error is in that mess. :frowning:

So here is my problem:
When I create my node, this work just fine and give me a nice print out.
MyNode::MyNode()
{
//DEBUG
cout << “Begin: MyNode” << endl;
}

BUT this is not working:
MyNode::~MyNode()
{
//DEBUG
cout << “Begin: ~MyNode” << endl;
MDGModifier tempDGMod;
NodeAboutToBeDeletedCB(thisMObject(), tempDGMod, &mCallbackIds);
}

So, my problem is that I want some code to be called in NodeAboutToBeDeletedCB() when I delete my maya node BUT nothing happens…
So I added this pretty //DEBUG cout<< to see if the destructor was called, and it seams it is not.

So I need your help.
Thx.

Node destructors are not called when a node is deleted from the scene. Maya retains information on the node because of the undo queue. See the docs:

http://download.autodesk.com/us/maya/2009help/index.html?url=Dependency_graph_plugins_The_basics.htm,topicNumber=d0e640879

For what you’re trying to do, the best thing would be to add a MDGMessage::addNodeRemovedCallback and then check if the node being deleted is of type “MyNode” or whatever your node is.

Neil

Just to clarify:

You want to have something run just before the Node is deleted?

You are making the CB in the destructor, which technically means the item has already been sent to memory collection, so the callback will never fire off, which seems to be what you currently are experiencing.

What I’d gather is you want to put the callback creator (addNodeRemovedCallback) in postcontruct, and when the object is about to be deleted it will fire off your callback, do what you want, then follow through to it’s destructor.

[QUOTE=Matt;9135]Hey guys,
I am trying to learn C++ and Maya Api all at once [/QUOTE]

That’s your bug, right there.

You need to learn C++ before learning how it works in a complex system like a 3D application.

The mistake you made is something no real developer would ever confuse (an object’s destructor and when a node is removed from the scene). I’ve also seen some pretty atrocious code from people that have gotten too deep into complex systems without understanding the already complex languages they use (like serializing an object in the destructor…).

Just a word of warning and some advice.

Thx for the help,
The bug I was attempting to fix was indead the fact that some of the commands where in the destructor plus another crutial chunk of code that was skipped in certain condition.

And just to clarify, this ain’t my code :wink:

But you are absolutely right Rob,
I need to learn C++ asap if I am to poke around MayaApi bugs :D: