Managing Memory with the FBX SDK Manager
 
 
 

Creating an FBX SDK Manager

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

KFbxSdkManager* lSdkManager = KFbxSdkManager::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 KFbxSdkManager to allocate and release memory. In the following example, the KFbxSdkManager is passed as a parameter to the KFbxScene::Create() function to instantiate a new scene.

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

Memory Allocation

The KFbxSdkManager will automatically allocate a sufficient amount of memory to contain the new object. Though not necessary, the memory allocation strategy of the KFbxSdkManager can be customized by passing your own KFbxMemoryAllocator as a parameter in KFbxSdkManager::SetMemoryAllocator().

Naming FBX Objects

In the code snippet above, the second parameter of KFbxScene::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.

NoteThe current implementation of the FBX SDK is not guaranteed to be thread-safe. Objects which are created using the same KFbxSdkManager, 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 KFbxScene 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 KFbxNode and KFbxNodeAttribute classes to define and manipulate scene elements. For now, we will only look at how a KFbxScene can be used to create these objects.

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

NoteIt is possible to create scene elements (KFbxNode, KFbxNodeAttribute) using only a reference to the KFbxSdkManager (instead of KFbxScene), 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 KFbxSdkManager is destroyed.

Destroying objects

An FBX SDK object should be explicitly destroyed by calling its Destroy() member function. The KFbxSdkManager will automatically free the memory allocated for that object, and will update all the internal connections between that object (KFbxObject), its properties (KFbxProperty), and other KFbxObject 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.