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:

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 :

7

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.

4

It seems to be a double deletion of the same pointer, and the root cause is in this line:

view_ = new QGraphicsView(this);

Here you create an object view_ and set its parent, however later you also add this object to the layout: layout_->addWidget(view_);. When your Window destroys, it deletes the layout, and the view, but layout also tries to delete the same view as its own child. To fix this, just create your view like:

view_ = new QGraphicsView;

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật