Delete docs create with IsolateObjects? [SOLVED]



  • On 27/05/2015 at 06:51, xxxxxxxx wrote:

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

    ---------
    I have this bit of code:

    BaseDocument* newDoc;
    AtomArray* ar = AtomArray::Alloc();
    ar->Append(b_source);

    newDoc=IsolateObjects(doc,*ar);

    Should I release the AtomArray ar and delete the newDoc created by the IsolateObjects command?
    Or will it all be deleted once my code finishes?



  • On 27/05/2015 at 09:41, xxxxxxxx wrote:

    You've allocated the AtomArray so it is your responsibility to Free() it.

    The caller owns the returned BaseDocument.  But, if you insert it using InsertBaseDocument(), C4D now owns it and you should not free it unless you remove it from the document list.



  • On 27/05/2015 at 10:45, xxxxxxxx wrote:

    I performed a Free for the AtomArray.
    But I only used the newDoc returned by the IsolateObjects to perform a BakeTexture()
    I never inserted the document in the document list.
    So, should I assume that it is destroyed once it goes out of scope?



  • On 27/05/2015 at 10:55, xxxxxxxx wrote:

    No.  It isn't on the stack and, although it was allocated by C4D (in the call), you now own it so you must free it after doing what you need.  One of those lovely things about C++ is that allocated memory must be managed since there is no standard garbage-collection as in other OOP languages.  AutoAlloc is one way to allow the memory to be freed automatically once it goes out of scope - but that won't work with the returned BaseDocument.  You might want to do that with the AtomArray, though.  Then you do not need to free it yourself after doing what you need with it.



  • On 27/05/2015 at 11:16, xxxxxxxx wrote:

    Ok, so I just perform a newDoc->Free(newDoc); when I don't need it anymore, right?
    As for the AtomArray, I need to use Alloc because I used .Append to add the object to it and AutoAlloc does not allow me to use .Append



  • On 27/05/2015 at 11:24, xxxxxxxx wrote:

    Not sure why you cannot call Append using AutoAlloc.  Try this:

    BaseDocument* newDoc;
    AutoAlloc<AtomArray> ar;
    if (!ar) return;  
    ar->Append(b_source);  
      
    newDoc=IsolateObjects(doc,*ar);  
      
    // blah, blah
      
    BaseDocument::Free(newDoc);
    


  • On 27/05/2015 at 11:27, xxxxxxxx wrote:

    Why all the potential memory leaks ?
    Why not use smart pointers ?

      
    AutoAlloc<AtomArray> ar; if(nullptr==ar) return false;  
    ar->Append(b_source);   
      
    AutoFree<BaseDocument> newDoc( IsolateObjects(doc,*ar) ); if(nullptr==newDoc) return false;  
    


  • On 27/05/2015 at 11:36, xxxxxxxx wrote:

    Never knew about AutoFree but that is pretty cool. 🙂



  • On 27/05/2015 at 11:58, xxxxxxxx wrote:

    Thank you, I'm still learning :-)

    I was not being able to .Append to AutoAlloc'ed AtomArry because I was trying to do the folowing:

    AutoAlloc<AtomArray> *ar;

    because previously I was coding:

    AtomArray* ar = AtomArray::Alloc();

    But it works like you guys told me :-D


Log in to reply