Freeing a shared object



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

    On 07/01/2008 at 13:18, xxxxxxxx wrote:

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

    ---------
    Howdy,

    I have an ObjectData plugin which contains an object pointer that is shared between the plugin instances (created for undo, etc.) :

    > _
    > class MyPlugin: public ObjectData {
    > public:
    >      myobject* obj; //shared object
    > }
    > _

    I have two problems:
    - When the plugin is copied, obj should be duplicated, is there a way to find whether the plugin object is actually copied (then there would be two MyPlugin objects in object manager) in CopyTo, or any other way to achieve this?
    - obj should be freed at the end, when it is not in use by and of the instances (eg. when user deletes the object from object manager), is there a way to check that in MyPlugin::Free or any other way to do it?

    Thanks in advance,
    Cheers,
    Miha



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

    On 07/01/2008 at 14:18, xxxxxxxx wrote:

    I think i've solved the first problem, didn't see the flags in CopyTo...



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

    On 08/01/2008 at 03:26, xxxxxxxx wrote:

    anyone?



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

    On 08/01/2008 at 11:48, xxxxxxxx wrote:

    What you're trying to do is a bit complicated - or, more specifically, a bit dangerous. CopyTo() is not only called when the user does a copy-paste but also when the document is being cache prepared or being copied for rendering. It is mainly used to maintain data not stored/handled by Cinema 4D (data other than BaseContainers and Descriptions) so that class elements can be copied as well, for instance.

    So if this object is being stored and maintained by your plugin, that's okay as long as it isn't also being permanently added to the document (in the Object Manager for instance). Now Cinema 4D has ownership and you just have a pointer or link to it. This could end up being a bit dangerous. Now you say that it is a shared object between multiple instances of your plugin. That seems even more dangerous unless you restrict actions to the first instance and pass them down to the other instances as necessary.

    I'm not quite sure what you are trying to achieve. If this object is to be just one shared object for all instances, you'll definitely need a flag in the plugin that allocates one when none exists and doesn't allocate others afterwards. Then you'll have to pass this shared object around to new instances of your plugin. Again, not sure if this is correct understanding.



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

    On 08/01/2008 at 13:10, xxxxxxxx wrote:

    Quote: Originally posted by kuroyume0161 on 08 January 2008
    >
    > * * *
    >
    > What you're trying to do is a bit complicated - or, more specifically, a bit dangerous. CopyTo() is not only called when the user does a copy-paste but also when the document is being cache prepared or being copied for rendering. It is mainly used to maintain data not stored/handled by Cinema 4D (data other than BaseContainers and Descriptions) so that class elements can be copied as well, for instance.
    >
    >
    >
    > * * *

    I am aware of CopyTo being called on many occasions and i think i did manage to handle that quite well. Haven't tried changing the object while rendering, which would obviously result in inaccurate rendering, but i guess i could exclude this and copied the object for that matter (as i do for copy/paste-ing the pluginobject in am)?

    > Quote: __
    >
    > * * *
    >
    > So if this object is being stored and maintained by your plugin, that's okay as long as it isn't also being permanently added to the document (in the Object Manager for instance). Now Cinema 4D has ownership and you just have a pointer or link to it. This could end up being a bit dangerous. Now you say that it is a shared object between multiple instances of your plugin. That seems even more dangerous unless you restrict actions to the first instance and pass them down to the other instances as necessary.
    >
    >
    >
    > * * *

    the object in question is an array of values, some sort of a cache that has to be shared between instances, since if it were copied it would be lost every time user would undo (as i understand this, if there is a way around this other then sharing the same object that would be even better). Copying is not an option since it uses quite a lot of memory.

    > Quote: __
    >
    > * * *
    >
    > I'm not quite sure what you are trying to achieve. If this object is to be just one shared object for all instances, you'll definitely need a flag in the plugin that allocates one when none exists and doesn't allocate others afterwards. Then you'll have to pass this shared object around to new instances of your plugin. Again, not sure if this is correct understanding.
    >
    >
    >
    > * * *

    This is correct. The object allocates only once, the problem is freeing it, since i have no idea when all the instances are gone (i think that might be when the document is closed, or even sooner if cinema has some undo limit, i'm not sure about that).

    Thanks,
    Cheers,
    Miha



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

    On 08/01/2008 at 13:34, xxxxxxxx wrote:

    Maintain a counter of instances. Unfortunately, for this type of thing you'll need to make the counter outside of the plugin (possibly global) - it has to be a singleton and can't be maintained independently in each plugin instance. As the counter goes to 0 (this should cover document closes) there will be no more instances and you can delete the shared object in that last instance as it goes away.

    Undos are a tricky-sticky beast very unfortunately. The object is added to an undo stack as-is and restored (upon undo) as-is. That means it may not reflect exact conditions. Let's say that you have three instances - so the counter reads '3'. You delete one instance - the counter reads '2'. When an undo is issued, the counter reads? '2' and you now have a disparity. The problem is that the SDK doesn't issue any worthwhile messages with undo/redo. There is only a MSG_DESCRIPTION_INITUNDO which is only good when ADDING undos - not for the actual undo process.

    One possible way around this is to use a SceneHook plugin to maintain the counter. It can periodically scan the document to rectify any disparities between reality and the counter value. It also avoids the need for a global counter element. Another solution might be to scan the document in the Free() method of the plugin to ensure that the instance is indeed the last instance and possibly rectify the counter value in the process. Obviously, keep a pointer to the counter element in each plugin instance. You can do this with a separate InitMyPlugin()-type method that is called after an instance is created, passing the counter to be stored in the instance.



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

    On 08/01/2008 at 16:32, xxxxxxxx wrote:

    I made that instance counter, but there is one weird problem (otherwise it works like a dream). It seems that whenever i import a file from attribute manager and undo that action CopyTo is called twice (flags of 4485 and 4992 on undo, only 4485 on import, no idea what this flags mean since they are not described in sdk), and one instance is NEVER freed. Might by any chance this be a c4d bug? I'm tracking all allocations and deallocations and there are obviously some instances that never get deallocated.



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

    On 09/01/2008 at 05:24, xxxxxxxx wrote:

    Quote: Originally posted by vuce on 08 January 2008
    >
    > * * *
    >
    > I made that instance counter, but there is one weird problem (otherwise it works like a dream). It seems that whenever i import a file from attribute manager and undo that action CopyTo is called twice (flags of 4485 and 4992 on undo, only 4485 on import, no idea what this flags mean since they are not described in sdk), and one instance is NEVER freed. Might by any chance this be a c4d bug? I'm tracking all allocations and deallocations and there are obviously some instances that never get deallocated.
    >
    >
    > * * *

    Looking further into this, the number of Init and Free calls is the same, but there is one CopyTo more (once on copyto there is no Init called!?). I have no idea how this is possible, if anyone has any ideas on this i'd be very grateful.

    Cheers,
    Miha


Log in to reply