Qut of memory in QVector



  • Good day everyone!
    I have such a problem - I need to subclass QAbstractTableModel to write my own one, which has capability to get data remotely from a server with REST. OK, a build is successful, but on app start this error rises:
    Out of memory in ..\..\include/QtCore/../../src/corelib/tools/qvector.h, line 551 terminate called without an active exception under Qt 5.9.6
    and
    Out of memory in ..\..\include/QtCore/../../src/corelib/tools/qvector.h, line 563 terminate called without an active exception under Qt 5.11
    taskmodel.h

    #include <QAbstractTableModel>
    #include "commons.h"
    
    class TaskModel : public QAbstractTableModel
    {
        Q_OBJECT
    
    public:
        TaskModel(QObject *parent = nullptr);
        ~TaskModel() override;
    
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    
        void setCredentials(const QString&, const QString&, const QString&);
    
    public slots:
        void suspendTask();
    
    private slots:
    
    private:
    
        QList<Task> *tasks = nullptr; // Внутреннее хранилище всех задач
    
        QString baseUrl;
        QString login;
        QString password;
    };
    

    taskmodel.cpp

    #include "taskmodel.h"
    
    TaskModel::TaskModel(QObject *parent) : QAbstractTableModel (parent) {}
    TaskModel::~TaskModel() {}
    
    int TaskModel::rowCount(const QModelIndex &parent) const {}
    
    int TaskModel::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return 3;
    }
    
    QVariant TaskModel::data(const QModelIndex &index, int role) const {}
    
    QVariant TaskModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if(role == Qt::DisplayRole) {
            if(orientation == Qt::Horizontal) {
                switch (section) {
                    case 0:
                        return QString("Код");
                    case 1:
                        return QString("Описание");
                    case 2:
                        return QString("В работе");
                }
            }
        }
        return QVariant();
    }
    
    void TaskModel::setCredentials(const QString &baseUrl, const QString &login, const QString &password)
    {
        this->baseUrl = baseUrl;
        this->login = login;
        this->password = password;
    }
    
    void TaskModel::suspendTask() {}
    

    mainwindow.h

    #include "cookieauthorizator.h"
    
    #include <QMainWindow>
    #include <QCloseEvent>
    #include <QSystemTrayIcon>
    #include "taskmodel.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(const QApplication& app, QWidget *parent = nullptr);
        ~MainWindow() override;
    
    protected:
        void closeEvent(QCloseEvent*) override;
        bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
    
    private slots:
        void iconActivated(QSystemTrayIcon::ActivationReason);
    
        void on_acGeneral_triggered();
    
    private:
        Ui::MainWindow *ui;
        QSystemTrayIcon *trayIcon;
        QMenu *menu;
        QAction *quit;
    
        TaskModel *model; 
    };
    

    mainwindow.cpp

    MainWindow::MainWindow(const QApplication &app, QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow), trayIcon(new QSystemTrayIcon(this)), menu(new QMenu(this)), quit(new QAction("Выход",this)), model(new TaskModel(this))
    {
        WTSRegisterSessionNotification(reinterpret_cast<HWND>(MainWindow::winId()), NOTIFY_FOR_THIS_SESSION); // Подписываем главное окно приложения на сообщения блокировки/разблокировки текущей сессии (проще говоря, экрана)
        ui->setupUi(this);
        ui->tvTask->setModel(model);
        menu->addAction(quit);
        trayIcon->setIcon(this->style()->standardIcon(QStyle::SP_ComputerIcon));
        connect(quit, &QAction::triggered, &app, &QApplication::exit);
        connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::iconActivated);
        trayIcon->show();
    }
    

    What am I doing wrong? I don't use QVector in my code directly, as you can see. The error rises in the guts of the framework class and I don't know why :(((((((
    P.S. Task is a simple struct like this in commons.h:

    struct Task {
    QString x;
    QString y;
    bool z;
    };
    

    P.P.S. Complier - MinGW 5.3.0 32bit; OS: Windows 10


  • Moderators

    @Tikani Did you try to run your app through debugger to see where the crash happens? In the code you posted I don't see anything obvious.



  • @jsulm yes, I tried, but error happens inside the Qt class, not in my code, so, debugger shows me an ugly assembler code. As far I understand, I need to rebuild the whole Qt with debug information/symbols to be able to get human-friendly debuggin proccess. I use Windows and default build environment from the opensource installator from the official Qt site (Qt 5.9.6 or Qt 5.11, MinGW 5.3.0 32bit) and rebuild Qt from source not under Linux - such a pain.


  • Moderators

    @Tikani But it could help to see where in your app you are when the crash happens and in which state the app is (values of variables, ...). Bugs in frameworks are rare, so most probably it is something in your app, but actual crash happens inside Qt (but not because of a Qt bug).



  • @jsulm Ok, then I try to rebuild Qt with debug information and post here a link to GitHub with my project (it's opensource) as well, may be then things get clearer for true Jedis :D may be there is a need in a big picture to resolve the problem.


  • Moderators

    @Tikani No! There is no need to rebuild Qt at this stage! I don't ask you to debug Qt - it will most probably not help you. Simply run your app through debugger until it crashes and see where in your app you are at that time.
    In most cases (from my own experience) if your app crashes somewhere in Qt it is NOT Qt but your app which is doing something wrong.


  • Moderators

    @Tikani Also you can post stack trace after crash here - it can help to find the issue



  • @jsulm OK, I got the point. I attach stacktrace and full code a little bit later - I need to go to work now :( Thanks anyway for the fastness of responses - now I'm confident that my issue would be resolved eventually with help of the community.


  • Qt Champions 2017

    Hi
    I see that you are using 32 bit.
    How many model items do you add ?


  • Lifetime Qt Champion

    Hi,

    @Tikani said in Qut of memory in QVector:

    QList<Task> *tasks = nullptr; // Внутреннее хранилище всех задач

    Why are you using a pointer for tasks ? I don't see it initialised anywhere.



  • @SGaist It is just an inner storage over which subclassed model works. I forgot to comment it after my experiments with fields and its types, when I tried to locate a culprit of a aprogram crash. Anyway, it doesn't initialize in any way, it is just empty nullptr junk pointer.
    By the way, here stacktrace comes:

    1  msvcrt!abort                                                                        0x7732b3fb 
    2  libstdc++-6!_ZN9__gnu_cxx27__verbose_terminate_handlerEv                            0x1768085d 
    3  libstdc++-6!_ZN10__cxxabiv111__terminateEPFvvE                                      0x17679399 
    4  libstdc++-6!_ZSt9terminatev                                                         0x17713930 
    5  qt_check_pointer                                         qglobal.cpp           3050 0x6b98abc1 
    6  QVector<QHeaderViewPrivate::SectionItem>::reallocData    qvector.h             551  0xde2dc07  
    7  QVector<QHeaderViewPrivate::SectionItem>::resize         qvector.h             416  0xde2e4ce  
    8  QHeaderViewPrivate::createSectionItems                   qheaderview.cpp       3530 0xdcd1a3d  
    9  QHeaderView::initializeSections                          qheaderview.cpp       2279 0xdcd2572  
    10 QHeaderView::initializeSections                          qheaderview.cpp       2205 0xdcd5918  
    11 QHeaderView::reset                                       qheaderview.cpp       1774 0xdcd9584  
    12 QAbstractItemView::setModel                              qabstractitemview.cpp 748  0xdcc0764  
    13 QHeaderView::setModel                                    qheaderview.cpp       408  0xdcd94e0  
    14 QTableView::setModel                                     qtableview.cpp        1136 0xdd140d6  
    15 MainWindow::MainWindow                                   mainwindow.cpp        15   0x401910   
    16 qMain                                                    main.cpp              11   0x4016f4   
    17 WinMain *16                                              qtmain_win.cpp        104  0x404d85   
    18 main                                                                                0x40934d   
    

  • Lifetime Qt Champion

    One more thing, your data method is empty, it should at least contain return QVariant();.

    You don't need to pass your QApplication instance as parameter. You can access it using the qApp macro.



  • @SGaist Thanks, the issue is solved. There was a need in return QVariant() indeed. Mark as closed.


  • Lifetime Qt Champion

    There's always a need to return something when your method has a return type. If you don't know what to return at first then return a default value.

    Also, always take into account the warnings the compiler throws at you (there is one for functions not returning anything when their signature contains a return type). As the old saying goes: a compiler warning ignored today is a catastrophic crash in production tomorrow.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.