4

I have a small problem with a custom node I created in Maya. If I do something like:

createNode -n "MyInstance" "MyNode";
delete "MyInstance";

It doesn't call the class destructor. But, if you do a new scene, open a new file, or do anything that forces the destruction of the current scene, the destructor is finally called.

The thing is that I have some memory allocations that I would like to clear right away instead of waiting until the end to actually clear them. Because they're not needed anymore, so they should be cleared.

I looked in the documentation, and I don't see any virtual functions I could override that would be called when the node is actually deleted, or a similar event. There's such function to track connection between attributes (actually using it in this case), but no such thing for delete.

So if any of you have an idea for a bypass, or a way to simulate this, please share!

Thanks.

W

widgg
  • 1,358
  • 2
  • 16
  • 35

1 Answers1

6

So guys,

Finally found it. It's totally not where it's supposed to be. Autodesk has some weird way to do things.

Here it is:

void removeMyNode(MObject &node, void *clientData)
{
  MFnDependencyNode nodeFn(node);
  MGlobal::displayInfo(MString("Removal callback node: ") + nodeFn.name());
}

MDGMessage::addNodeRemovedCallback(removeMyNode, "MyNode", NULL, &status);

So, if a node is deleted, this callback is actually called. Still, I would have preferred a way to do this directly within the node definition by overriding a function.

Thanks to everyone that took a look at this question. Hope this could help some of you eventually!

widgg
  • 1,358
  • 2
  • 16
  • 35
  • Is this sufficient as code, or do you have to call the specific destructor in the callback ? – Laurent Crivello Jan 26 '16 at 15:10
  • If you allocated anything in the constructor, or at any other point for that node, you need to deallocate it there. Particularly if internally, your data can be shared by multiple node. A bit similar to a shared pointer, make sure you decrease the count and delete if needed. – widgg Jan 26 '16 at 16:30
  • Ok because my issue looks like yours with a slightly different effect. When deleting the node (MPxNode) with he delete key, then Maya crashes. If I delete it via code (mdg.deleteNode) then it works. But interrupting the nodeRemoveCallback doesn't seem enough. – Laurent Crivello Jan 26 '16 at 16:56