QQuickView新删除类型不匹配

时间:2018-05-18 10:22:52

标签: c++ qt qml qquickview

使用g ++ -fsanitize = address编译时,我得到一个new-delete-type-mismatch。这段代码错了吗?或者这是Qt Bug?

我在一个大型应用程序中找到了这个并将其分解为下面的代码。我想用C ++ ++和QQuickView显示一个QML项目。但是QQuickView的析构函数似乎存在问题。

test.pro:

TEMPLATE = app
TARGET = test
QT += core gui widgets quick

SOURCES += test.cpp

QMAKE_CXXFLAGS += -fsanitize=address
QMAKE_LFLAGS += -fsanitize=address

TEST.CPP:

#include <QApplication>
#include <QQuickView>
#include <QQuickItem>

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

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("test.qml"));
    view.show();

   return app.exec();
}

test.qml:

import QtQuick 2.4

Item {
    id: item1
    anchors.fill: parent

    Rectangle {
        id: rectangle
        color: "#fba4e3"
        anchors.fill: parent
    }
}

编译:

$ qmake-qt5 test.pro
$ make

g++ -c -pipe -fsanitize=address -O2 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtQuick -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtQml -isystem /usr/include/qt5/QtNetwork -isystem /usr/include/qt5/QtCore -I. -isystem /usr/include/libdrm -I/usr/lib64/qt5/mkspecs/linux-g++ -o test.o test.cpp
g++ -fsanitize=address -Wl,-O1 -o test test.o   -lQt5Widgets -lQt5Quick -lQt5Gui -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread

执行命令

$ ./test

=================================================================
==6822==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x60f000034de0 in thread T0:
  object passed to delete has wrong type:
  size of the allocated type:   168 bytes;
  size of the deallocated type: 32 bytes.
    #0 0x7fc304be16d8 in operator delete(void*, unsigned long) (/lib64/libasan.so.4+0xe16d8)
    #1 0x7fc303547e07 in QObjectPrivate::deleteChildren() (/lib64/libQt5Core.so.5+0x27fe07)
    #2 0x7fc303548ce1 in QObject::~QObject() (/lib64/libQt5Core.so.5+0x280ce1)
    #3 0x7fc304234e44 in QQuickItem::~QQuickItem() (/lib64/libQt5Quick.so.5+0x1ece44)
    #4 0x7fc3042501a4  (/lib64/libQt5Quick.so.5+0x2081a4)
    #5 0x7fc3042d30f9 in QQuickView::~QQuickView() (/lib64/libQt5Quick.so.5+0x28b0f9)
    #6 0x40102d in main (/home/lukas/Programmieren/C++/Qt/test/test+0x40102d)
    #7 0x7fc3021a0889 in __libc_start_main (/lib64/libc.so.6+0x20889)
    #8 0x401199 in _start (/home/lukas/Programmieren/C++/Qt/test/test+0x401199)

0x60f000034de0 is located 0 bytes inside of 168-byte region [0x60f000034de0,0x60f000034e88)
allocated by thread T0 here:
    #0 0x7fc304be0158 in operator new(unsigned long) (/lib64/libasan.so.4+0xe0158)
    #1 0x7fc3039ddbf1 in QQmlType::create(QObject**, void**, unsigned long) const (/lib64/libQt5Qml.so.5+0x265bf1)
    #2 0x60f00003d2ff  (<unknown module>)

SUMMARY: AddressSanitizer: new-delete-type-mismatch (/lib64/libasan.so.4+0xe16d8) in operator delete(void*, unsigned long)
==6822==HINT: if you don't care about these errors you may set ASAN_OPTIONS=new_delete_type_mismatch=0
==6822==ABORTING

1 个答案:

答案 0 :(得分:3)

这确实是QtQuick中的一个已知错误,但仍未解决,请查看此bug report

其中一条评论提供了一些关于原因的提示,这显然与QML引擎使用带有展示位置目标的new这一事实相关,该目标的大小大于对象的大小目前正在建设中。这可能是出于优化原因而做的,所以不知道这是否会被解决,但它看起来没有害处。