Reference Object Lifetime Management
Objects managed by the 3ds reference system, are normally destroyed automatically when there are no more strong references to them. One exception to this are nodes (INode
) which can be deleted by a user. Occasionally user code needs to destroy objects derived directly or indirectly from class Animatable
, ReferenceMaker
, or ReferenceTarget
.
When these objects are destroyed by calling the delete operator on a pointer to them, the reference links between these objects are not properly disconnected, which can make 3ds Max unstable.
To make it easier for plug-ins to use the correct method of destroying objects via pointers to class Animatable
, ReferenceMaker
or ReferenceTarget
, the destructor of these classes has been made protected. As a side effect, instances of these classes cannot be stack based. That means they cannot be allocated on the stack or passed by value as function parameters.
Never use operator delete directly on a plug-in instance, or any instance of classes derived from class Animatable
, class ReferenceMaker
, and class ReferenceTarget
. Instead use one of the following techniques for deleting objects:
- Objects derived from
ReferenceTarget
should be deleted by callingReferenceTarget::MaybeAutoDelete()
. This is going to be the most common scenario, as most 3ds Max entities are derived fromReferenceTarget
. - Objects derived from
ReferenceMaker
and notReferenceTarget
should be deleted by callingReferenceMaker::DeleteMe()
. - Objects that derive from
ReferenceTarget
but overwriteReferenceTarget::AutoDelete()
to prevent their deletion should be deleted by callingReferenceMaker::DeleteMe()
. - Objects derived from
Animatable
but are not derived fromReferenceMaker
should be deleted by callingAnimatable::DeleteThis()
.
When an object is deleted via ReferenceTarget::MaybeAutoDelete()
or ReferenceMaker::DeleteMe()
all of the dependent are notified, the reference are deleted, and undo is handled properly.