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

    AutoAlloc problem

    Scheduled Pinned Locked Moved SDK Help
    14 Posts 0 Posters 1.3k Views
    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 Offline
      Helper
      last edited by

      THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

      On 09/12/2011 at 18:04, xxxxxxxx wrote:

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

      ---------
      Hi,

      Can anyone tell me why AutoAlloc does not work in this example:

          //AutoAlloc<PolygonObject> mypoly(4,1); //This does not work!!?  
        
        PolygonObject *mypoly = PolygonObject::Alloc(4,1); if(!mypoly) return NULL;  //This does work  
        //mypoly->ResizeObject(4,1);                                                 //Only used if changing an existing polygon  
        
        Vector *gp = mypoly->GetPointW();  
              
        gp[0] = Vector(-100,0,-100);   // Place the first point here  
        gp[1] = Vector(-100,0,100);    // Place the first point here  
        gp[2] = Vector(100,0,100);     // Place the first point here  
        gp[3] = Vector(100,0,-100);    // Place the fourth point here  
        
        doc->InsertObject(mypoly, NULL, NULL);  
        
      // Create the new poly based on the points  
        CPolygon *p = mypoly->GetPolygonW();  
        p->a = 0;   
        p->b = 1;  
        p->c = 2;  
        p->d = 3;  
        p->d = mypoly->GetPointCount()-1;    //<---Creates a quad polygon  
        //p->a = mypoly->GetPointCount()-1; //<---Creates a tri polygon if desired  
         
        mypoly->Message(MSG_UPDATE);  
        
        
        EventAdd();
      

      When I use Alloc it works just fine. And creates a new polygon.
      But when I use AutoAlloc. The polygon stays stuck in memory. And won't insert into the OM.
      I don't understand why that's happening.

      -ScottA

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

        THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

        On 10/12/2011 at 01:54, xxxxxxxx wrote:

        As I understand it, if you use AutoAlloc the object is deleted the moment it goes out of scope. So even if you insert it into a document, it'll be deleted as soon as your function ends. Not sure if that explains what you're seeing?

        If you do:

        doc->InsertObject(mypoly->Release(), NULL, NULL);

        it might work.

        Steve

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

          THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

          On 10/12/2011 at 07:02, xxxxxxxx wrote:

          Howdy,

          From the sdk documentation:

          Note:  If you ever need to give away the allocated object to any other function you cannot use AutoAlloc. It won't release the ownership, so the object will still be deleted when your function exits.

          Adios,
          Cactus Dan

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

            THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

            On 10/12/2011 at 07:47, xxxxxxxx wrote:

            Hi Dan,

            Are you saying you can't release objects allocated through AutoAlloc? I thought that's what it (the Release function) was for? But I could well be wrong!

            Steve

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

              THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

              On 10/12/2011 at 08:00, xxxxxxxx wrote:

              Howdy,

              Originally posted by xxxxxxxx

              ...Are you saying you can't release objects allocated through AutoAlloc?...

              No, I'm simply quoting the documentation.

              I interpreted that note in the documentation as advice from the developers on when to use AutoAlloc and when not to use it. 😉

              Adios,
              Cactus Dan

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

                THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                On 10/12/2011 at 08:18, xxxxxxxx wrote:

                Thanks for the help guys.

                I was confused because I've been using AutoAlloc just fine to create things that exist just like a polygon does:
                -AutoAlloc<Gradient> gradient;
                -AutoAlloc<BaseBitmap> bmp;
                -AutoAlloc<BaseDocument>tempdoc;
                -etc...
                So I didn't understand why it wasn't working with polygons too.

                To be honest. I wanted to avoid using Alloc mainly because I'm not very confident in how to free things yet. So that I don't end up with any memory leaks.
                I've seen a few ways to write it and I'm not sure I'm doing it correctly.

                In my example.
                Are either of these two examples the correct syntax to free my polygon object called mypoly:
                1.-void Free(PolygonObject*& mypoly);
                2.-mypoly->Free(*& mypoly);

                -ScottA

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

                  THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                  On 10/12/2011 at 08:25, xxxxxxxx wrote:

                  Howdy,

                  To free your polygon object you'd use the first method, but the syntax is like this:

                  PolygonObject::Free(mypoly);
                  

                  Adios,
                  Cactus Dan

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

                    THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                    On 10/12/2011 at 08:26, xxxxxxxx wrote:

                    Thanks.🍺

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

                      THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                      On 10/12/2011 at 08:46, xxxxxxxx wrote:

                      Whoops...One last question about this.
                      If I create a polygon object like this. And the user closes the scene and opens a new one with C4D still open. Is the polygon still in memory?

                      Should I use something like: if(!currentdoc) PolygonObject::Free(mypoly);
                      Or will C4D handle the dumping of the polygon memory for me when the use closes the current scene?

                      -ScottA

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

                        THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                        On 10/12/2011 at 09:02, xxxxxxxx wrote:

                        Howdy,

                        Well, that's something you'll probably have to determine based on your code's program flow.
                        What kind of plugin is it? I mean if the polygon object is held in memory as a class member variable pointer (for example like a member variable of a TagData plugin), then you can free it in that class's Free() function. This way if the tag is in the document, but the polygon object is not inserted into the document and held in memory only, when the tag is deleted (whether manually or by closing the document) so will the polygon object be freed.

                        Adios,
                        Cactus Dan

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

                          THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                          On 10/12/2011 at 09:33, xxxxxxxx wrote:

                          You bring up a good point that I hadn't thought about yet concerning polygons being held in tags Dan.
                          But in this case. I'm just creating a polygon from the command() method in a GeDialog class.
                          I was wondering if anything is left in memory that needs to be freed. If the user closes the scene and opens a new one?

                          I'm guessing C4D will flush it all. But I wanted to be sure.

                          -ScottA

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

                            THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                            On 11/12/2011 at 06:04, xxxxxxxx wrote:

                            Howdy,

                            Well, if the PolygonObject is only needed while the dialog is open and it is a member variable of your dialog class, you could use the GeDialog::DestroyWindow() function to free it from memory.

                            Adios,
                            Cactus Dan

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

                              THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                              On 11/12/2011 at 10:00, xxxxxxxx wrote:

                              My main problem is I've never had to worry about memory management in the other languages I use. And now I'm having a hard time figuring out the specific way Free() needs to be used in C4D plugin classes.

                              In raw C++ tutorials they teach it like this.
                              Where something is created in memory and then destroyed all in the same code block scope:

                              int main ()  
                              {  
                                int * buffer1, * buffer2, * buffer3;  
                                buffer1 = (int* ) malloc (100*sizeof(int));  
                                buffer2 = (int* ) calloc (100,sizeof(int));  
                                buffer3 = (int* ) realloc (buffer2,500*sizeof(int));  
                                free (buffer1);  
                                free (buffer3);  
                                return 0;  
                              }
                              

                              But if I do that in a C4D plugin like this:

                              Bool myDialog::Command(LONG id,const BaseContainer &msg)   
                              {  
                                BaseDocument *doc = GetActiveDocument(); //Get the active document  
                                
                                IconData idata;                    //Create an IconData type variable to hold our custom registered icon's ID#  
                                idata.bmp = BaseBitmap::Alloc();   //Create a BaseBitmap variable...Images are loaded via an instance of the the BaseBitmap class  
                                Filename fn = GeGetPluginPath()+Filename("Res")+Filename("myicon.png");   //The file path where the icon image is located    
                                if(idata.bmp->Init(fn,-1,NULL) != IMAGERESULT_OK) return FALSE;           //Error handling  
                                                  
                                idata.w = idata.bmp->GetBw();     //Get the icon image's width  
                                idata.h = idata.bmp->GetBh();     //Get the icon image's height  
                                GetIcon(200000000,&idata);        //Get our custom registered icon. The ID# is set up in the "RegistermyResDialog(void)" function  
                                GetCustomIconData *myIconData =((GetCustomIconData* )&idata); //Create a CustomIconData type variable called "myicon"      
                                myIconData->filled=TRUE;          //Tells C4D that the state of the icon is "filled"  
                                  
                                switch (id)   
                                  {  
                                     //Do some stuff in here if gizmos were used  
                                  }  
                                
                                EventAdd();  
                                
                                BaseBitmap::Free(idata.bmp); //<---Crashes!!!  
                                return TRUE;  
                              }
                              

                              It crashes C4D.

                              I think I can see where this would be a problem. Because the dialog window would be still open while the Free() function executes. Which would be bad.
                              So I think what I need to do is free things created with Alloc only after the dialog window is closed.
                              But I can't find any examples on how to do that in the SDK. The examples all seem to use AutoAlloc<> in them.

                              -ScottA

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

                                THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

                                On 11/12/2011 at 15:34, xxxxxxxx wrote:

                                Never mind.

                                This line of code is too advanced for me to figure out how to free it from memory: idata.bmp = BaseBitmap::Alloc();

                                I kept getting memory leaks from my bitmap code. Based upon the c4d_debug.txt debugger.
                                So I found another way to do it using AutoAlloc:

                                    AutoAlloc<BaseBitmap> myicon;  
                                  IconData idata;                    //Create an IconData type variable to hold our custom registered icon's ID#  
                                  idata.w = myicon->GetBw();         //Get the width of the Icon if needed later  
                                  idata.h = myicon->GetBh();         //Get the height of the Icon if needed later
                                

                                It seems to work just as well this new way.
                                And frees the memory properly now.

                                Thanks for the help,
                                -ScottA

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