Managing Memory with the FBX SDK Manager

Creating an FBX SDK Manager

The FbxManager class is responsible for creating, managing, and destroying FBX SDK objects. Only one FbxManager instance is required for a given program. The creation of this FbxManager singleton is typically the first action of any FBX SDK program.

FbxManager* lSdkManager = FbxManager::Create();

Creating Objects with the FBX SDK Manager

Objects in the FBX SDK are created or destroyed by means of their respective Create() and Destroy() member functions. At a lower level, these functions rely on the FbxManager to allocate and release memory. In the following example, the FbxManager is passed as a parameter to the FbxScene::Create() function to instantiate a new scene.

FbxScene* lScene = FbxScene::Create(lSdkManager, "Scene Name");

Memory Allocation

The SDK object manager (FbxManager) automatically allocates a sufficient amount of memory to contain the new object. Though not necessary, the memory allocation strategy of the object manager can be customized using the handler setter functions such as FbxSetMallocHandler located in fbxalloc.h. See the ViewScene/main.cxx example for information about how to use a custom memory allocator.

Naming FBX Objects

In the code snippet above, the second parameter of FbxScene::Create() is a string. When objects are created, a string can be used to specify the name of the new object. This string does not have to be unique, and it is acceptable to pass an empty string "". Object names can facilitate the debugging of FBX SDK applications and their outputs.

NOTE:The current implementation of the FBX SDK is not guaranteed to be thread-safe. Objects which are created using the same FbxManager, but that are accessed in different threads will likely cause the application to crash. This is a known issue, and there are plans to ensure the thread-safety of the FBX SDK in a future release.

Creating Objects within a Scene

A FbxScene object can contain a variety of scene elements such as meshes, lights, animations, characters, etc. These elements should be created with a reference to the scene in which they exist. As such, when the scene is exported, so are all of its elements. When the scene is destroyed, the memory allocated to all of its objects is also released. The Nodes and the Scene Graph section describes the use of the FbxNode and FbxNodeAttribute classes to define and manipulate scene elements. For now, we will only look at how a FbxScene can be used to create these objects.

// Create a node object
FbxNode* lNode = FbxNode::Create(lScene, "node");
 
// Create a mesh object
FbxMesh* lMesh = FbxMesh::Create(lScene, "");

NOTE:It is possible to create scene elements (FbxNode, FbxNodeAttribute) using only a reference to the FbxManager (instead of FbxScene), however when the scene is destroyed, those scene elements will not be destroyed with it; they will only be destroyed either explicitly or when the FbxManager is destroyed.

Destroying objects

An FBX SDK object should be explicitly destroyed by calling its Destroy() member function. The FbxManager will automatically free the memory allocated for that object, and will update all the internal connections between that object (FbxObject), its properties (FbxProperty), and other FbxObject to remove any inconsistencies. For more information on the concept of connections in the FBX SDK, see Connections. The following code snippet illustrates how to destroy the objects we instantiated above.

// Destroy these objects
lMesh->Destroy();      // Destroy the mesh
lNode->Destroy();      // Destroy the node
lScene->Destroy();     // Destroy the scene and its objects
lSDKManager->Destroy() // Destroy SDK Manager and any remaining objects which it manages.