THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 22/11/2010 at 12:29, xxxxxxxx wrote:
Cinema 4D Version:
Language(s) : C++ ;
can someone tell me how to use AutoAlloc to create for example a nullobject? i am facing a bunch of memory leaks and have no idea where they come from. so i thought AutoAlloc may help to get further??
thanks in advance,
On 22/11/2010 at 22:47, xxxxxxxx wrote:
AutoAlloc creates a temporary memory object which is automatically freed at the end of the scope (when the method in which it is used returns). If you only use the nullobject temporarily and don't mind it being freed under these conditions you can do so. If you insert it into a BaseDocument then you must nullobject->Remove() it before the method is exited unless you want a crash or something.
Memory leaks are tough business but the meat and potatoes of programming without garbage collection or other automated forms of memory allocation/freeing. You must always check who owns the memory object and always free it when it is no longer of use - which can be under several circumstances such as when it is no longer needed, when the class instance to which it is attached is itself freed, and so on.
"Memory as a Programming Concept in C and C++" by Frantisek Franek is probably the best little (but somewhat expensive) book you can ever get to understand how memory management works and should be approached and utilized.
On 22/11/2010 at 22:50, xxxxxxxx wrote:
AutoAlloc only helps if you want the Object to be destroyed at the end of the scope. BaseObjects are usually inserted to Documents or returned by the creating function. So that won't help you in these cases. You would need to check where the objects are allocated and then free them as soon as you don't need them anymore.
edit: Robert was faster
On 22/11/2010 at 23:07, xxxxxxxx wrote:
thank you both.. i'll once again look over the code to see where the leaks appear.
On 23/11/2010 at 02:41, xxxxxxxx wrote:
Your descriptions sound quite negative but
AutoAlloc and the other Auto... classes are very usefull and you should use them whenever you can. For example you can have multiple exit points in your functions and not worry about freeing of some temporary data.
You can of course release the memory from automatic management via the Release function. It returns the pointer and i think sets the Autos internal pointer to NULL. If it is a BaseObject for example you could put it into a document without problem. Like doc->Insert(autoop.Release());
Have a look at this
On 23/11/2010 at 11:03, xxxxxxxx wrote:
ok, after all, my initial question remains, how do i use AutoAlloc, what would it look like if i want to do this SplineObject::Alloc(4,Tlinear) with AutoAlloc, or this BaseObject::Alloc(Oextrude) ??
thanks for you time
On 23/11/2010 at 15:08, xxxxxxxx wrote:
Michael is correct. You can use the Release() method to remove ownership of the allocated memory from AutoAlloc. I can see how this can be useful if you want automated freeing with a premature error exit from a method but where you can then wrestle ownership so that it is now owned by you or some other object and not AutoAlloc. Thereafter, you take responsibility for freeing the memory if not owned by some other object which takes responsibility!
Using AutoAlloc with initializing parameters is explained in the SDK docs:
_It is possible to pass parameters to theAlloc() function via AutoAlloc's constructor:
__ AutoAlloc<VariableTag> hermite(Thermite2d, 100);_
So, you would call, for isntance:
_ AutoAlloc<VariableTag> hermite(Thermite2d, 100);_
AutoAlloc <SplineObject> mySpline(4, Tlinear);
if (!mySpline) return whoops();
On 24/11/2010 at 11:58, xxxxxxxx wrote:
thank you for the help.