Manually plugin instantiation



  • On 03/08/2015 at 10:55, xxxxxxxx wrote:

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

    ---------
    Greetings !

    I have created a CommandData plugin, which opens a dialog when pressed.
    So i have created a class that inherits both CommandData and Dialog.

    class MyPlugin : public CommandData, Dialog {
    //code
    }

    I know they can be separated but i find no reason for not keeping them together.
    Now, this plugin contains some data lets say int A and B.

    I try to use this plugin as singleton, by creating a private static variable MyPlugin instance.
    It also has a constructor which causes crash when called at the start of the program.

    The crash is located in Dialog's constructor when trying to allocate the object. 
    this keyword returns 0.

    I assume you cant instantiate plugin classes manually right ?
    It works fine when i use public static accessor methods for plugin's data.

    Thank you.



  • On 03/08/2015 at 12:10, xxxxxxxx wrote:

    Can't do that.  Cinema 4D's API does not support multiple inheritance (period).  You will need to keep them separated.



  • On 03/08/2015 at 16:31, xxxxxxxx wrote:

    Actually, it does work. You have to declare the parent-classes virtual. Anyway, I would not suggest
    this form, it is hard enough to keep track of which method comes from what class in the inheritance,
    don't make it ever more confusing.

    enum
    {
      PLUGIN_ID = 1000009,  // TEST ID
    };
      
    class TestCommand : virtual public CommandData, virtual public GeDialog
    {
    public:
      
      virtual Bool Execute(BaseDocument* doc)
      {
        return this->Open(DLG_TYPE_ASYNC, PLUGIN_ID);
      }
      
      virtual Bool CreateLayout()
      {
        this->AddStaticText(0, 0, 0, 0, "Hello! This is a multiple inheritance test.", BORDER_ROUND);
        return true;
      }
      
    };
    


  • On 03/08/2015 at 23:59, xxxxxxxx wrote:

    Greetings!

    Keeping track of which method belongs to each class isn't a problem.
    What i would like to know is whether or not this is well-defined with c4d classes.

    Also, why do i have to declare the parent classes virtual ? They don't have any common class as parent.
    What's the best practice to use plugin's data in other classes as well.

    Thank you for your time and your replies.



  • On 04/08/2015 at 02:16, xxxxxxxx wrote:

    Hello,

    I agree that multiple inheritance is nothing one should do in that specific case; this is not the typical way of handling that situation. And personally I don't see any benefit of doing this.

    Please be also aware that you should not create static instances of complex Cinema 4D types. With static variables you never know exactly when they will be created. If this happens before Cinema's memory system or some other subsystem is available, it must crash.

    Best wishes,
    Sebastian



  • On 04/08/2015 at 03:17, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    Also, why do i have to declare the parent classes virtual ? They don't have any common class as parent.
    What's the best practice to use plugin's data in other classes as well.

    You're right, it works even without the virtual  keyword. 😊 The crash you experience might be caused
    by what Sebastian described. You can't have a static String  or other complex class from the Cinema 4D
    API, as it becomes constructed before the function table (C4DOS) is passed from C4D to the plugin.

    -Niklas



  • On 04/08/2015 at 05:32, xxxxxxxx wrote:

    Thank you very much for your answers.

    I have separated Command Data and Dialog. I store the plugin data in CommandData subclass (whatever is used in the dialog). They can be accessed in other parts of code using static methods. I usually use singletons, but since i cant create static instances that inherit C4D classes, it doesnt seem to be another way.

    Thnx again.



  • On 04/08/2015 at 07:06, xxxxxxxx wrote:

    There should be no problem creating  a static CommandData object. However

    a) if the CommandData subclass has a complex class member, there's the same problem again
    b) if you use RegisterCommandPlugin(), Cinema 4D expects you  to have allocated the object
    using NewObjClear() , since it will use DeleteObj()  when it shuts down to delete it.

    -NIklas



  • On 11/08/2015 at 03:48, xxxxxxxx wrote:

    Greetings !

    Thnx for your answers.


Log in to reply