On 02/03/2014 at 11:07, xxxxxxxx wrote:
Originally posted by xxxxxxxx
Hi Robert.
So when people say that they save data in the document using their own custom container. What they really mean is that they are creating a new sub container inside of the doc->GetDataInstance() container?
Yes.
Originally posted by xxxxxxxx
Also.
Who are the owners of these containers that I'm making from scratch?:
BaseContainer myBc1;
BaseContainer *myBc2 = Null;
In the first case, the owner is scope (the container will be created on the stack and removed once it goes out of scope). When you SetContainer(), the data (not the container itself) is then owned by the parent container but if you allocate the BaseContainer, you still need to free it. Note that there is no SetContainer() with a pointer - the container is 'instantiated' as data in the parent container and you still own the original (as it were).
Originally posted by xxxxxxxx
When I create these container inside of plugins. The plugin is the owners right?
The reason I'm wondering about this is what if I do these in a CommandData plugin?
A CD plugin only runs once then it closes. So I'm wondering about the ownerships.
Does the container belong to the CD plugin?
Does the container get deleted after the CD plugin closes? Or does it still hang around in memory?
When you set any data to a BaseContainer, it exists as long as there is a 'physical form' (an instance). In the case of anything that belongs to a BaseDocument, it will exist in there as long as the document is opened and longer if saved to disk. In the case of a CommandData, only during a C4D session unless you save data using SetWorldPluginData() and GetWorldPluginData() (again, to disk).
Ownership here isn't the same as transferring pointers from my experience. If you allocate a BaseContainer onto the heap (id est: with gNew or GeAlloc() et al), even if you set it somewhere, you retain ownership of that memory and must free it. Cinema 4D 'recreates' the container otherwise and, I would imagine, handles ownership of that new instance internally.
The odd thing about BaseContainers is that they don't typically work like regular pointer instances. You can get a pointer to one, but you never own that. You can declare/allocate one, set it up, and pass it somewhere (in SendModelingCommand() or as a sub-container or numerous other ways), but C4D doesn't track the memory, only the data as a sort of clone of the BaseContainer. You own that instance whether scope-based on the stack or memory-based on the heap.