Paul Giaccone wrote:
Peter Dimov wrote:
Paul Giaccone wrote:
1. The creator function, required by Maya. Note the absence of a
smart
pointer. I tried wrapping this in a shared_ptr and returning .get() but this caused a crash, if I remember rightly.
void* MyNode::creator(void) { return new MyNode(); }
This is, I think, exactly as it should be. Maya expects to take ownership >>of the returned pointer and will delete it when it decides that it's no longer needed. But I'm not an expert on that; you probably need to ask in a Maya SDK forum to be sure.
A bit of research and feedback from HighEnd 3D's Maya API forum suggests that this is the case - you create the node and Maya takes responsibility deleting it when necessary. Apparently, Maya owns the node and deletes it itself when it feels like it, not when the programmer says so.
If I've understood correctly, this means that any memory allocated on the heap in MyNode must be done using bare pointers rather than smart pointers, so that Maya can delete it when it wants, not when the smart pointers say the memory can be released. MyNode and any objects that it
allocates on the heap still need to have destructors to deallocate their contents, but it is up to Maya when the destructor for MyNode is called.
Not an ideal state of affairs (once you've experienced smart pointers, you never want to go back to the inconvenience of having to work out where the all the deletes need to go), but then I've found that Maya is
hardly the most programmer-friendly system, IMO.
First, let me say that I am not familiar with Maya, so I may not know what I am talking about. That said, are there still some objects you need to delete manually? I think you can still use smart pointers for those, and save the raw pointers for objects you pass back to Maya (like the pointer returned from MyNode::creator). The only problem I can see with this is if you have an object that Maya MIGHT delete for you, or that you might need to delete yourself. If you get to deal with that kind of ugliness, maybe you can use a custom deleter that checks some sort of flag before deleting the object. If you can put this flag in the object itself, then it is a simple matter of setting it before you give the object to Maya. If you can't, then it would probably be easier to use raw pointers for that object.