Search code examples
c++qtqt6

Crash when creating QT QList


I am trying to create a simple QT QList of type QLine, but every time I compile & run it on Windows I get an exception: enter image description here

The call stack is:

>   basic_example.exe!QArrayDataPointer<QLine>::~QArrayDataPointer<QLine>() Line 112    C++ 
    basic_example.exe!QArrayDataPointer<QLine>::reallocateAndGrow(QArrayData::GrowthPosition where, __int64 n, QArrayDataPointer<QLine> * old) Line 250 C++ 
    basic_example.exe!QArrayDataPointer<QLine>::detachAndGrow(QArrayData::GrowthPosition where, __int64 n, const QLine * * data, QArrayDataPointer<QLine> *old) Line 210    C++ 
    basic_example.exe!QtPrivate::QPodArrayOps<QLine>::emplace<QLine const &>(__int64 i, const QLine & <args_0>) Line 179    C++ 
    basic_example.exe!QList<QLine>::emplaceBack<QLine const &>(const QLine & <args_0>) Line 886 C++ 
    basic_example.exe!QList<QLine>::append(const QLine & t) Line 459    C++

The code is in the drawBackground function of a QGraphicsScene.

Below is a minimum viable program that can re-create the issue I am having, I don't believe I am doing anything out of the ordinary.

#include <cstdlib>
#include <QtWidgets/qapplication.h>
#include<QtWidgets/qgraphicsview.h>
#include<QtWidgets/qwidget.h>
#include<QtWidgets/qboxlayout.h>
#include <QtWidgets/qgraphicsscene.h>
#include <QtGui/qpainter.h>

class GraphicsScene : public QGraphicsScene {
public:
    GraphicsScene (QGraphicsScene* parent = nullptr) : QGraphicsScene (parent) {
        int scene_width = 64000;
        int scene_height = 64000;

        // Set the focus to be in the middle of the view when it's created.
        setSceneRect (-scene_width / 2,
            -scene_width / 2,
            scene_width,
            scene_height);
    };

private:
    void drawBackground (QPainter* painter, const QRectF& rect) {
        QGraphicsScene::drawBackground (painter, rect);

        QList<QLine> lines;
        QLine line_that_crashes = QLine (0, 0, 100, 100);
        lines.append (line_that_crashes);
    }
};

class Window : public QWidget {
public:
    Window (QWidget* parent = nullptr) : QWidget (parent) {
        setGeometry (200, 200, 1024, 768);
        setWindowTitle ("Minimum Window");

        layout_ = new QVBoxLayout ();
        layout_->setContentsMargins (0, 0, 0, 0);
        setLayout (layout_);

        graphics_scene_ = new GraphicsScene ();
        view_ = new QGraphicsView (this);
        view_->setScene (graphics_scene_);
        layout_->addWidget (view_);

        show ();
    }

private:
    QGraphicsScene* graphics_scene_;
    QBoxLayout* layout_;
    QGraphicsView* view_;
};

int main (int argc, char *argv[]) {

    QApplication application(argc, argv);

    Window wnd;

    return application.exec();
}

Debug : enter image description here


Solution

  • This is a result of mixing debug and release binaries, Qt is using different heaps for allocation and deallocation.

    Qt Release binaries should be linked with your application only in release mode, and a Debug build should only link debug binaries of Qt. Don't mix debug and release binaries

    CMake should handle this on its own and link the correct versions, also QtCreator can generate proper CMake files that you can use in other IDEs, avoid linking things manually and use a proper build system.