Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

All the API methods which return object pointers allocate those objects and pass ownership to your code. You are responsible for freeing them when they are no longer needed. Since they are allocated in a different module, you cannot call the delete operator on them. Instead, you must call the DeleteThis() method, which all the objects inherit from the MaxwellMayaAPIObject base class. To simplify this task, the API defines a minimalistic smart pointer template called MaxwellMayaPtr which can wrap raw pointers to API objects. Instances of the smart pointer call DeleteThis() on the wrapped object when they go out of scope. For convenience, the API also defines an instantiation of this template for every class. The name of the smart pointer instantiation is always the name of the wrapped class followed by Ptr, or ConstPtr for wrappers of const pointers. For example, the smart pointers wrapping MaxwellMayaCamera are:

typedef MaxwellMayaPtr<MaxwellMayaCamera> MaxwellMayaPtr<MaxwellMayaCamera>        MaxwellMayaCameraPtr;
typedef MaxwellMayaPtr<const MaxwellMayaCamera> MaxwellMayaCamera>  MaxwellMayaCameraConstPtr;

If you ever need to pass ownership of an API object outside a function, and the object is wrapped in a smart pointer, you can use the Detach() method of the MaxwellMayaPtr class. For example, if you want to write a function which creates and returns a mesh, you can do something like this:

MaxwellMayaMesh* MakeMesh()
{
    MaxwellMayaMeshPtr mesh(api->CreateMesh(/* arguments */));
    if(!mesh)
    {
        // MaxwellMayaPtr can wrap NULL pointers. In that case, it simply does nothing when it goes out of scope.
        return NULL;
    }
    if(!mesh->SetPositions>SetPositions(/* arguments */))
    {
        // You can just return and the smart pointer will correctly destroy the mesh handle.
        return NULL;
    }
    // Pass ownership of the raw pointer to the caller. Detach() sets the internal pointer to
    // NULL, so nothing happens when the smart pointer exists the scope.
    return mesh.Detach();
}

...