Documents in memory [SOLVED]



  • On 28/02/2016 at 20:47, xxxxxxxx wrote:

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

    ---------
    Hi Folks,

    I have an object plugin that's holding some documents in memory. There's an option for the user to 'edit' the document by making it the current working doc. I'm doing this with the following function:

      
    bool MyPlugin_Object::Library_EditDoc(int id)   
         {   
              GePrint("Library_EditDoc(" + LongToString(id) + ") called..");   
      
              if(id > NULL && id < Library.size())   
              {   
                   if(Library[id].Doc != nullptr)   
                   {                       
                        BaseObject *obj = BaseObject::Alloc(ID_MYPLUGIN_OBJECT);   
                        MyPlugin_Object *plug = (MyPlugin_Object* )obj;   
                        plug->Set_EditDocumentFlag(TRUE);   
                           
                        BaseDocument *doc = Library[id].Doc;   
      
                        GePrint("pre insert flag: " + LongToString(plug->GetEditDocumentFlag())); // OK   
                        doc->InsertObject(obj,nullptr,nullptr,FALSE);   
                        GePrint("post insert flag: " + LongToString(plug->GetEditDocumentFlag())); // WRONG VALUE   
      
                        InsertBaseDocument(doc);   
                        SetActiveDocument(doc);   
                           
                        return TRUE;   
                   }   
      
                   GePrint("Reference is nullptr..");   
      
                   return FALSE;   
              }   
                 
              GePrint("id out of range, returning");   
      
              return FALSE;   
         }   
    

    The issue I'm having is that when I insert the object using doc->InsertObject(...) the flag changes (and often returns a value of 144). It's a private member bool. I've tried a few variations of CopyTo() but neither seem to make any difference. Examples below.

      
    Bool MyPlugin_Object::CopyTo(NodeData* dest,GeListNode* snode,GeListNode* dnode,COPYFLAGS flags,AliasTrans* trn)   
         {   
              MyPlugin_Object *plug_dest = (MyPlugin_Object* )dest;   
                 
              // EXAMPLE 1:   
              plug_dest->Set_EditDocumentFlag(EditDocument);   
                 
              // EXAMPLE 2:   
              plug_dest->EditDocument = EditDocument   
                 
              return TRUE;   
         }   
    

    Both ways build fine. I've tried debugging the code, and depending on a few ways I make the Library_EditDoc() function I either get a spinning wheel, or it gets pulled up with no code to debug. It does go through and becomes the active working doc at times, but the flag is wrong. I've gone around the place with this, making clones of the doc and inserting into them instead. But I always get an error. It always seems to happen when using InsertObject(), or something related to this it seems.

    What might be causing this, or where am I going wrong here? Cheers,

    WP.



  • On 29/02/2016 at 01:34, xxxxxxxx wrote:

    Hello,

    I guess "EditDocument" is the document owned by the object plugin? In your "CopyTo" function you don't copy that document, you just hand over the pointer. This means it is now completely unclear who owns that instance and what happens when any of the object instances in memory gets deleted.

    Also InsertBaseDocument() takes over the ownership of the given BaseDocument. If you use this function you must release the ownership of the given BaseDocument. If you don't, the ownership is unclear and when one owner deletes the document this will lead to any kind of error.

    So make sure that the ownership of everything you handle is always clear. And in "CopyTo" you really have to copy the document.

    Best wishes,
    Sebastian



  • On 01/03/2016 at 01:18, xxxxxxxx wrote:

    Hi Seb,

    the 'EditDocument' flag is just a bool flag, not the document link. The original reference is held in a struct.

    I've gone back to working with a clone of the document, I think this is a better way of handling it. So there's two, the original I keep in memory, and the clone that's in the viewport. The one in the viewport is copied back to the one in memory when the user has finished with it.

    To test if it was something with my object, I created a new object (complete new code and plugin id) with the bare basics, and just added a private BaseDocument link variable. I added this object to the cloned scene and set the object's parent document link through a function. The problem is the same. The reference link is reset to null, even though it's not touched anywhere else in the code (CopyTo() is never called anyway).

    Is there something offside that could affect this? Some strange object registration feature!? Something else? I really have no idea where to look..!

    WP.



  • On 01/03/2016 at 01:39, xxxxxxxx wrote:

    Hello,

    there is one thing I just see in your code: you allocate your object with BaseObject::Alloc() but then you cast it into "MyPlugin_Object". I guess "MyPlugin_Object" is your NodeData and ObjectData based class?

    BaseObject::Alloc() will allocate a BaseObject, not a ObjectData object. To get the NodeData instance from a BaseObject you have to use GetNodeData().  Also this is why one should use static_cast instead of C-casts.

    See also "NodeData – what is it good for?"

    Best wishes,
    Sebastian



  • On 01/03/2016 at 02:53, xxxxxxxx wrote:

    Eh.....!! I've stumped myself on this one before I think!

    Thanks Sebastian, it just needed a sharp eye! The casting was the issue. All is working as expected, the document reference is now valid.

    Cheers,

    WP.


Log in to reply