Memory Allocation

3DS Max Plug-In SDK

Memory Allocation

See Also: Class Animatable, Dll Functions and Class Descriptors, Class ClassDesc.

Overview

The 3ds max API provides a method used to allocate memory when a new instance of a plug-in class is required. It also provides several different ways to deallocate this memory depending on the plug-in type. In the most common case, these methods are ClassDesc::Create() and Animatable::DeleteThis().

Methods

When 3ds max needs to create a new instance of a plug-in class, it calls the Create() method of the class descriptor. This method returns a new instance of the plug-in class. The memory for the class may be allocated by using the new operator as shown below:

void *Create(BOOL loading = FALSE) { return new MyObject; }

For example, when 3ds max loads a file from disk it needs to create instances of the plug-ins that are used (the geometric objects, lights, cameras, materials, controllers, etc.). It does this by calling ClassDesc::Create().

The memory must be freed when 3ds max is done with the item. Usually a method named DeleteThis() is called. This method is called to free the memory associated with the plug-in class. Since the memory was allocated with the new operator, it must be de-allocated using the delete operator as shown below:

void DeleteThis() { delete this; }

For plug-ins that are part of the Animatable class hierarchy, DeleteThis() is a method of Animatable. For plug-ins that are not derived from Animatalbe there may be a non-inherited DeleteThis() method. For example, the UtilityObj class used to create Utility plug-ins is not derived from Animatable and has its own DeleteThis().

The DeleteThis() method also gives the developer control over deleting. For example, a Utility Plug-In object may be statically declared and not actually declared in the heap. A utility plug-in such as this would implement DeleteThis() to do nothing since there is no heap memory to free.

void DeleteThis() {}

This is just what the \MAXSDK\SAMPLES\UTILITIES\ASCIIOUT.CPP code does.

A few plug-in types have no DelteThis() method at all -- 3ds max deletes the memory directly. The Image Filter and Compositor plug-in types (subclassed from ImageFilter) are allocated using ClassDesc::Create() but are deleted by the system. In this case the plug-in has no DeleteThis() method to implement. The system just calls delete on the plug-in internally (using the delete operator).