About
Primitive data types and structures can be created on the stack. Complex data types, classes and dynamic memory have to be allocated using Cinema 4D's memory management system. Using Cinema 4D's memory management system increases the stability and speed of a plugin.
- Warning
- The creation of objects and references in the context of the MAXON API ALIASES is described here: Entity Creation.
Object Allocation
New instances of generic classes should be allocated and freed with these functions:
- NewObj(): Creates an object with the given constructor parameters.
- DeleteObj(): Deletes the given object.
- Note
- Always check for
nullptr
when allocating memory.
if (newObject != nullptr)
{
const Int32 number = newObject->GetNumber();
}
- Note
- None of these functions will throw an exception in case of an error.
Alloc and Free
Most complex classes of the classic API provide a static "Alloc" and "Free" function. These functions have to be used to create new instances of these classes.
if (cube == nullptr)
if (doc == nullptr)
{
}
- Note
- It is recommended to use AutoAlloc (see below) as much as possible to avoid memory leaks.
AutoAlloc
AutoAlloc is a smart pointer that allocates and deallocates a new instance of a class using its static "Alloc" and "Free" functions based on scope.
- Note
- There is only a limited number of AutoAlloc constructors. If no available constructor fits use the class' "Alloc" function and manage the ownership with AutoFree.
if (cube == nullptr)
if (doc == nullptr)
AutoFree
AutoFree is a smart pointer that deallocates the stored object with its static "Free" function based on scope.
if (cube == nullptr)
cubeFree->
SetName(
"The new cube"_s);
if (doc == nullptr)
Further Reading