inherited shared pointer failed to get_widget

57 Views Asked by At

I am using a base class in c++ to imitate interface in Java, such I don't have to repeat some code or create object for every different class I create.

Basically it contains the RefPtr to a GtkBuilder, that I will use throughout the application.

But I find that the program terminates whenever I access it from the inherited class.

class SetupUIClass{
public:
    SetupUIClass(std::string builderResourceString, Glib::ustring baseWidgetName){
    }
    SetupUIClass(Glib::RefPtr<Gtk::Builder> builder, Glib::ustring baseWidgetName){
        this->builder = builder2;
        //Point A
    }
protected :
    Glib::RefPtr<Gtk::Builder> builder;
}
class MyApplicationWindow: public SetupUIClass, public Gtk::ApplicationWindow{
public:
    MyApplicationWindow(std::string builderResourceString, Glib::ustring baseWidgetName){
        SetupUIClass(builderResourceString, baseWidgetName);
        Glib::RefPtr<Gtk::Builder> builder2 = Gtk::Builder::create_from_resource(builderResourceString);
        //Point B

        MyApplicationWindow(builder2, baseWidgetName);

    }
    MyApplicationWindow(Glib::RefPtr<Gtk::Builder> builder2, Glib::ustring baseWidgetName){
        //Point C
    }
}

So the variable builder holds the pointer to the gtkbuilder.

I use builder->get_widget(widgetname, somewidgetpointer) to check if my program is running ok.

At Point B I can do builder2-> which is using the locally created pointer, program continues to run.

Then program goes into Point A, where I called the super constructor, At Point A, I can do both builder2-> and this->builder->, which are respectively the pointer passed into the constructor and the protected variable, program continues to run.

however when I reach point C, where it can only access the inherited protected pointer, when I do this->builder->get_widget, program stops without any output or error thrown.

I am scratching my head over this. Is there something I did wrong?

  • Inherited class cannot access address pointed to by the inherited protected pointer?
  • the Refpointer cleaned itself and lifecycle of the gtk builder is over?
  • the address changed as going from one class to another?

Any help will be appreciated. Or may be please point out if I am doing something wrong the entire time.

UPDATE I did some further checking, if(builder) returned false in point C but not point A, that is what caused the problem. But shouldn't I have stored the builder variable already in the superclass constructor?

in the Glib documentation it states it allows copying.

1

There are 1 best solutions below

0
Jimmy Chi Kin Chau On

seems I was misled by some other stack posts of the syntax of calling base class constructor. after using probably proper initialization method

MyApplicationWindow::MyApplicationWindow(Glib::RefPtr<Gtk::Builder> builder2, Glib::ustring baseWidgetName):SetupUIClass(builder2, baseWidgetName){

instead of

MyApplicationWindow(std::string builderResourceString, Glib::ustring baseWidgetName){
    SetupUIClass(builderResourceString, baseWidgetName);

that particular problem seems solved.