Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SegFault accessing `ui->tabWidget` but there is warning about vtable entries



  • Warnings I'm getting and trying to correct:

    dd3bb940-5107-421d-96ca-e85c0a9a528d-image.png

    Upon building a second time, it builds but segfaults in this function:

    void MainWindow::zoomCurrentWidget(int times)
    {
        auto widget = ui->tabWidget->currentWidget();
    
        if (widget) {
            auto view = dynamic_cast<ZoomableView*>(widget);
    
            if (view)
                view->zoomTimes(times);
        }
    }
    

    My mainwindow.h is :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        void addTabWidget(QWidget* widget, const QString& tabName);
    
    private slots:
        void on_actionZoom_In_triggered();
        void on_actionZoom_Out_triggered();
    
        void zoomCurrentWidget(int times);
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    

    I've also tried: virtual ~MainWindow() and virtual ~MainWindow() override and ~MainWindow() override. All the same issues.

    It is supposed to be straightforward to work with UI Forms, but I'm getting segfaults...


  • Lifetime Qt Champion

    Then you killed the tabWidget somewhere else already.


  • Lifetime Qt Champion

    Where do you initialize ui?



  • MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    

    Code generated by Qt Creator. Also, it's not because I have zoomCurrentWidget under private slots . I put it under public slots and same error.



  • @enjoysmath
    For this kind of problem, have you at least tried emptying out the whole build directory and rebuilding from scratch? Perhaps not your case, but this has solved quite a few "funnies" from other people in the past, worth ensuring.



  • @JonB Nope that didn't solve it :)



  • @enjoysmath

    1. click make clean and rebuild your app( better cd the build dir and do make distclean, then click run qmake)
    2. Set a break point at the beginning of that func and show which line causes crashes.

  • Lifetime Qt Champion

    Hi
    It all looks pretty normal.

    If you open one of the examples and run. Does it compile without any such errors ?

    Do you link any other libs via the pro file ?

    I have seen this error message with some nasty pointer errors.
    Do you do anything interesting with any of the Widgets ?
    Like keeping them in a list and also insert into a form or anything like that ?



  • @mrjj Nope, nothing unusual. What's causing the vtable errors?


  • Lifetime Qt Champion

    Hi
    well actually the normal one is
    undefined reference to `vtable for
    which can be caused by not implementing virtual functions or lack
    QOBJECT macro among other things.

    But this error
    cant find Linker symbol for virtual table
    seems to be linked to both virtual function and dangling pointers.

    If i had to guess , it would be on some kind of pointer error.

    When it crashes, the call stack gives no hint on what it was doing ?



  • 
     void MainWindow::zoomCurrentWidget(int times)
     {
         auto widget = ui->tabWidget->currentWidget();
     
         if (widget) {
             auto view = dynamic_cast<ZoomableView*>(widget);
     
             if (view)
                 view->zoomTimes(times);
         }
    }
    

    @mrjj It's in this function where the SegFault happens. It was usually on the first line, but later the Qt Creator had me believing it was also happening further down.



  • @mrjj It's definitely the first line. It doesn't return a working pointer to the QTabWidget, although QtCreator, when you hover over the code in Debug mode, shows that it's a QTabWidget* typed value. So there is a bug with QtCreator. I'm doing nothing special to induce this bug. Does this mean I have to go back to PyQt5 to finish my project? I thought C++ would be a lot nicer for handling large graphics scenes, but apparently there are just too many bugs.


  • Lifetime Qt Champion

    Then you killed the tabWidget somewhere else already.



  • @Christian-Ehrlicher that's the first time I'm accessing the QTabWidget.



  • @Christian-Ehrlicher

    Okay, you were right :)

    I was doing this:

    ```
    

    MainWindow w;
    w.setCentralWidget(&view);
    w.show();

    
    Which would of course wipe out the QWidget created from the UI Designer.  Thank you!
    
    :)

  • Lifetime Qt Champion

    @enjoysmath
    Hi
    Just as a note:

    • Does this mean I have to go back to PyQt5 to finish my project?

    Python version is a binding using the c++ classes so if it was a Qt bug same thing would happen in PyQt5.



  • @mrjj @Christian-Ehrlicher @JonB @JonB

    Thanks all who helped me solve this. I will make it my duty to write an awesome software tool for my users.

    -EnjoysMath


Log in to reply