Managing memory with the SDK manager
 
 
 

Class KFbxSdkManager (sometimes called the SDK manager) is the memory manager for FBX SDK. You will use it, directly or indirectly, whenever you instantiate an FBX SDK object, and again whenever you destroy an FBX SDK object. An FBX application normally:

Normally, an FBX application needs only one SDK manager object.

Almost all classes in FBX SDK have a Create() member function and a Destroy() member function. Whenever you call these functions, SDK Manager automatically looks after allocating and releasing memory.

Creating an SDK manager

This code snippet declares an SDK manager object and then instantiates it:

KFbxSdkManager* mySdkManager = NULL;
...
mySdkManager = KFbxSdkManager::Create();

Using SDK manager to create a scene object

When you create a scene object, you must pass the SDK manager object that will manage memory for that scene:

// Create a scene object
KFbxScene* myScene = KFbxScene::Create(mySdkManager, "");

Most FBX applications need only one scene. But if, for example, you wish to load (i.e., import) several FBX files into memory and work with them all at the same time, you can create a separate scene for each file.

NoteYou can use FBX SDK to import and export FBX files as well as other formats. See File formats imported and exported

Using a scene object to create other FBX objects

Most classes in FBX SDK have a Create() member function which you use in place of a constructor.

To instantiate (create) an object of most FBX classes:

  1. Call the class’s Create() member function.
  2. For the first parameter, pass the scene object to which the object belongs.

For example:

// Create a node object
KFbxNode* myNode = KFbxNode::Create (myScene, "");
 
// Create an mesh object
KFbxImporter* myMesh = KFbxMesh::Create (myScene, "");

The scene that you pass as a parameter knows to which SDK manager it belongs. That SDK manager is responsible for allocating memory for the node, mesh, or other FBX object that you are instantiating.

NoteWhen you export a scene to a file, all objects created with the scene object will be exported.

Specifying names for FBX objects

The second parameter of Create() is an optional name for the object. You can specify any name as a string: the name does not need to be unique. You can also specify an empty string. Examples:

// Create a scene object named My Scene
KFbxScene* myScene = KFbxScene::Create(mySdkManager, "My Scene");
 
// Create a node object named My Own Node
KFbxNode* myNode = KFbxNode::Create (myScene, "My Own Node");
 
// Create a mesh object named My Very Own Mesh
KFbxMesh* myMesh = KFbxMesh::Create (myScene, "My Very Own Mesh");
 
// Create a camera object with no name
KFbxCamera* myCamera = KFbxCamera::Create (myScene, "");

Because the names of FBX objects need not be unique, you cannot simply “get” an FBX object by specifying its name. But you can write code to enumerate all the objects in a scene that have a specified name.

Creating FBX objects that belong to no scene

If for any reason you wish to create an object that is not a part of any specified scene, you can pass an SDK manager object rather than a scene object:

// Create a camera manipulator object to be used in several scenes
KFbxCameraManipulator* myCameraManipulator =
    KFbxCameraManipulator::Create (mySDKManager, "");
NoteConsider an application with two FBX scenes in memory, scene1 and scene2. If an object has been created with scene1 as the first parameter, and then scene2 is exported, the object will not be exported. If the object has been created with the SDK manager as the first parameter, then the object will not be exported when either scene1 or scene2 is exported.

Destroying objects

Always destroy an FBX object using its Destroy() member function. SDK Manager will automatically free all memory allocated for the object. SDK Manager will also update all connections to that object that are stored in other FBX objects (see Connections).

To destroy any object created with Create():

// Destroy these objects
myMesh->Destroy();      // Destroy the mesh
myNode->Destroy();      // Destroy the node
myScene->Destroy();     // Destroy the scene and its objects
mySDKManager->Destroy() // Destroy SDK Manager and its objects

Notes on destroying an object:

To clean up memory after using FBX SDK:

This will also destroy any remaining SDK objects managed by that SDK Manager.

// Destroy the FBX SDK manager.
// All the objects that
// (1) have been allocated by the memory manager, AND that
// (2) have not been explicitly destroyed
// will be automatically destroyed.
if (mySdkManager) mySdkManager->Destroy();

Specifying your own memory allocation routines

NoteSkip this topic unless your application is using its own memory management routines.

FBX implements its own memory allocation routines (i.e., malloc, calloc, realloc, free, and msize). By default, whenever you create() an FBXobject, SDK Manager allocates and manages memory for that object using the FBX memory allocation routines.

If you want FBX SDK to use another set of routines, call KFbxSdkManager::SetMemoryAllocator before creating any KFbxSdkManager object. SetMemoryAllocator() takes a KFbxMemoryAllocator object as a parameter.