Allocating memory

On 04/09/2017 at 03:45, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R14 
Platform:   Windows  ;   
Language(s) :     C++  ;

Hi Folks,

looking for some clarification on allocating and deallocating memory.

When should an ObjectData inherited class destroy its data? Should this be done in the destructor, or should I be overriding Free()? Something else?

Similar question for the constructor. In my custom classes I set all pointers to nullptr in the constructor, and usually allocate the memory elsewhere (I setup my own initialiser functions). But in my ObjectData plugins I do this in the constructor. Should I be doing this in the Init() function? I read a post here from a few years a go that said the Init() might be called more than once - why might this happen? And should I therefore be setting up tests inside the Init() function?


Edit - spelling error!

On 05/09/2017 at 07:45, xxxxxxxx wrote:

Hi WP,

while you technically can use constructor and destructor for such purposes, we strongly recommend not to do so. Rather use Init() and Free(). The reason is the return value of Init(), so other parts of Cinema can react to for example allocation failures and an object not initializing correctly thereof.

See also the manual on NodeData::Init().

Init() may indeed be called several times for example for object instances on the undo stack (while not being the exact same instances, they still somewhat refer to the same object). If this causes problems for your implementation you will need to handle this yourself. See also the manual on the Undo System.