-
首页
-
C4D R23.110 C++ SDK
Entity Creation and Destruction Manual (Classic)
内容表
关于
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.
-
警告
-
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.
-
注意
-
Always check for
nullptr
when allocating memory.
// This example allocates a new instance of the given object class.
// A constructor parameter is handed over.
// After the object is used it will be deleted.
CustomClass* newObject =
NewObj
(CustomClass, 123)
iferr_return
;
if (newObject !=
nullptr
)
{
const
Int32
number = newObject->GetNumber();
ApplicationOutput
(
"Number: @"
, number);
DeleteObj
(newObject);
}
-
注意
-
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.
// This example allocates a BaseObject with BaseObject::Alloc().
// If the object cannot be inserted into a document it will be deleted.
BaseObject
* cube =
BaseObject::Alloc
(
Ocube
);
if
(cube ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
if
(doc ==
nullptr
)
{
BaseObject::Free
(cube);
return
maxon::NullptrError(
MAXON_SOURCE_LOCATION
);
}
doc->
InsertObject
(cube,
nullptr
,
nullptr
,
true
);
-
注意
-
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.
-
注意
-
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
.
// This example allocates a BaseObject with AutoAlloc.
// If something goes wrong one can leave the function and AutoAlloc will free the BaseObject.
// If everything goes right the ownership of the BaseObject is handed over
// to the given BaseDocument.
AutoAlloc<BaseObject>
cube {
Ocube
};
if
(cube ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
cube->
SetName
(
"The new cube"
_s);
// if something is wrong, just return
if
(doc ==
nullptr
)
return
maxon::NullptrError(
MAXON_SOURCE_LOCATION
);
// release ownership and insert into the document
BaseObject
*
const
releasedCube = cube.Release();
doc->
InsertObject
(releasedCube,
nullptr
,
nullptr
,
true
);
AutoFree
AutoFree
is a smart pointer that deallocates the stored object with its static "Free" function based on scope.
// This example allocates a BaseObject with BaseObject::Alloc().
// The ownership is given to an AutoFree object that
// will delete the object when the scope is left.
BaseObject
*
const
cube =
BaseObject::Alloc
(
Ocube
);
if
(cube ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
// AutoFree takes ownership
AutoFree<BaseObject>
cubeFree;
cubeFree.
赋值
(cube);
cubeFree->
SetName
(
"The new cube"
_s);
// if something is wrong, just return
if
(doc ==
nullptr
)
return
maxon::NullptrError(
MAXON_SOURCE_LOCATION
);
// release ownership and insert into the document
BaseObject
*
const
releasedCube = cubeFree.
发行
();
doc->
InsertObject
(releasedCube,
nullptr
,
nullptr
,
true
);
延伸阅读
void InsertObject(BaseObject *op, BaseObject *parent, BaseObject *pred, Bool checknames=false)
#define DeleteObj(obj)
定义:
newobj.h:159
void Assign(TYPE *p)
定义:
ge_autoptr.h:225
#define iferr_return
定义:
resultbase.h:1434
#define Ocube
Cube.
定义:
ge_prepass.h:1040
#define MAXON_SOURCE_LOCATION
定义:
memoryallocationbase.h:66
TYPE * Release()
定义:
ge_autoptr.h:214
void SetName(const maxon::String &name)
定义:
c4d_baselist.h:2324
maxon::Int32 Int32
定义:
ge_sys_math.h:58
#define ApplicationOutput(formatString,...)
定义:
debugdiagnostics.h:207
static BaseObject * Alloc(Int32 type)
#define NewObj(T,...)
定义:
newobj.h:108
static void Free(BaseObject *&bl)