gpt4 book ai didi

c++ - 当 C++ 尝试访问 vector 的元素(在范围内)时,Qt Quick Windows 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 05:34:36 26 4
gpt4 key购买 nike

我在 Windows 上的 Qt Quick 应用程序遇到了非常奇怪的崩溃。

我有一个名为 QObjectVectorQObject 派生 c++ 类,它允许 QML 端访问容器中的 QObject 指针。在 QML 方面,我有一堆按钮显示该容器中对象的名称。当我滚动鼠标滚轮时,我手动更新每个按钮的 currentItem 属性。

奇怪的是,只有有时,在不可预测 次数的快速滚动之后,程序崩溃(停止工作)。调试后发现,当at函数访问m_data[i]时会发生崩溃。我已经确保 i 在有效范围 [0, m_data.size()) 内,因此它不是索引超出范围错误。我的猜测是,不知何故,QML 引擎在尝试管理内存时删除了我在堆上的对象。

QObjectVector.h

#pragma once

#include <QObject>
#include <QVector>

#include "Types.h"

namespace LPP
{
class QObjectVector : public QObject
{
Q_OBJECT

Q_PROPERTY(Int size READ size NOTIFY sizeChanged)

public:
explicit QObjectVector();
virtual ~QObjectVector();

Int size();
Q_INVOKABLE QObject* at(Int);
void push(QObject*);
void remove(Int);
void remove(QObject*);

QVector<QObject*> &getData();

bool deleteChildrenOnDestroy;

private:
QVector<QObject*> m_data;

signals:
void sizeChanged();

public slots:
};
}

QObjectVector.cpp

...
QObject* QObjectVector::at(Int i)
{
qDebug() << "size: " << this->m_data.size();
for (int i = 0; i < this->m_data.size(); i++){
qDebug() << "i: " << i << " item: ";
//when I scroll rapidly, crash happens here when I trace objects in vector
qDebug() << this->m_data[i];
}
qDebug() << "returning.";
return (i >= 0 && i < this->m_data.size()) ? this->m_data[i] : nullptr;
}
....

按钮容器.qml

....
//this function is called when I scroll mouse wheel.
//itemList contains list of Buttons

function refreshDisplay() {
var i, j = 0;

for (i = 0; i < itemList.length; i++){
itemList[i].visible = itemList[i].enabled = false;
}

var start = Utils.clamp(Math.floor(scrollView.flickableItem.contentY / (itemHeight + itemVSpacing)), 0, currentFolder.size);
var end = Utils.clamp(Math.ceil((scrollView.flickableItem.contentY + scrollView.flickableItem.height) / (itemHeight + itemVSpacing)), 0, currentFolder.size);

var item;

for (i = start; i < end; i++){
if (j >= itemList.length){
itemList.push(tableItem_comp.createObject(tableArea));
}

itemList[j].visible = itemList[j].enabled = true;

item = currentFolder.at(i);
itemList[j].currentItem = item;
j++;
}
}
....

按钮.qml

....
property var currentItem: null;

anchors.left: parent.left
anchors.right: parent.right

SimpleButton {
id: button
width: 100
height: 50
text: currentItem.name;
onClicked: {
viewer.select(currentItem);
}
}
....

这个问题非常奇怪和不可预测,解决它对我的应用程序的开发非常关键,所以请拜托,任何帮助将不胜感激。

谢谢!

汤米

最佳答案

这是因为您要返回的对象归 QML 引擎所有,因此可以随时进行垃圾回收。

试着打电话

QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership)

在每个对象返回到 QML 之前(例如,当它被构造时)。

参见 this section文档:

When data is transferred from C++ to QML, the ownership of the data always remains with C++. The exception to this rule is when a QObject is returned from an explicit C++ method call: in this case, the QML engine assumes ownership of the object, unless the ownership of the object has explicitly been set to remain with C++ by invoking QQmlEngine::setObjectOwnership() with QQmlEngine::CppOwnership specified.

Additionally, the QML engine respects the normal QObject parent ownership semantics of Qt C++ objects, and will not ever take ownership of a QObject instance which already has a parent.

关于c++ - 当 C++ 尝试访问 vector 的元素(在范围内)时,Qt Quick Windows 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38592752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com