Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Delete docs create with IsolateObjects? [SOLVED]

    SDK Help
    0
    9
    743
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • H
            Helper
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • H
              Helper
              last edited by

              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

              1 Reply Last reply Reply Quote 0
              • H
                Helper
                last edited by

                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);
                
                1 Reply Last reply Reply Quote 0
                • H
                  Helper
                  last edited by

                  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;  
                  
                  1 Reply Last reply Reply Quote 0
                  • H
                    Helper
                    last edited by

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

                    Never knew about AutoFree but that is pretty cool. 🙂

                    1 Reply Last reply Reply Quote 0
                    • H
                      Helper
                      last edited by

                      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 😄

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post