使用g++ -fsanitize = address进行编译时,出现了new-delete-type-mismatch。此代码是否错误?还是这是Qt错误?
我在一个大型应用程序中发现了这一点,并将其分解为以下代码。我想用QQuickView从C++显示一个QML项目。但是QQuickView的Destructor中似乎有一个问题。
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
最佳答案
这确实是QtQuick中的一个已知错误,仍未解决,请检查此bug report。
其中的一条评论提供了有关该原因的一些暗示,该原因显然与以下事实有关:QML引擎使用new
和放置目标,该目标的大小大于当前正在构建的对象。这样做可能是出于优化方面的原因,因此不知道是否可以解决此问题,但这看起来没有害处。