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 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();
}
maxon::Int32 Int32
Definition: ge_sys_math.h:56
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
#define DeleteObj(obj)
Definition: newobj.h:159
#define NewObj(T,...)
Definition: newobj.h:108
#define iferr_return
Definition: resultbase.h:1524
- 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)
{
}
doc->InsertObject(cube,
nullptr,
nullptr,
true);
Definition: c4d_baseobject.h:248
static BaseObject * Alloc(Int32 type)
static void Free(BaseObject *&bl)
#define Ocube
Cube.
Definition: ge_prepass.h:1119
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
const char * doc
Definition: pyerrors.h:226
- 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)
doc->InsertObject(releasedCube,
nullptr,
nullptr,
true);
Definition: ge_autoptr.h:37
void SetName(const maxon::String &name, Bool setDirty=true)
Definition: c4d_baselist.h:2549
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);
doc->InsertObject(releasedCube,
nullptr,
nullptr,
true);
Definition: ge_autoptr.h:155
TYPE * Release()
Definition: ge_autoptr.h:223
void Assign(TYPE *p)
Definition: ge_autoptr.h:234
Further Reading