QML 列表视图和分段错误

QML ListView and Segmentation fault

本文关键字:分段 错误 视图 列表 QML      更新时间:2023-10-16

我正在使用带有SectionScrollerQAbstractListModel的QML ListView。我注意到当我通常滚动(不使用SectionScroller(时,我在memcpy中遇到分段错误(从未显式调用(

你知道为什么会这样吗?

我试图重现它,现在分段错误是

0x402f9c3a??(( from/usr/lib/libQtScript.so.4
0x402f9c3a: LDRH R1, [R7, R3]

调试符号在那里,尽管没有转储有价值的信息。另一次赛格错误是

0x0000cab8 in QBasicAtomicInt::ref (this=0x0( at/usr/include/QtCore/qatomic_armv6.h: 119

这很奇怪,因为 AFAIK N900 的处理器是 armv7/edit:在 N950 上它使用相同的处理器,并且在 Qt 源中仅用于 ARM qatomic_arm.hqatomic_armv6.h所以它应该没问题。

ListView{
    id: irrview
    width: parent.width
    model: irregulars
    anchors.top: caption.bottom
    anchors.bottom: parent.bottom
    spacing: 5
    clip: true
    section.criteria: ViewSection.FirstCharacter
    section.property: "form0"
    delegate: Rectangle{
        id: del
        property  int fontSize: 20
        height: 60
        width: parent.width
        color: "#E0E1E2"
        Row{
            height: parent.height
            width: parent.width - 10
            anchors.horizontalCenter: parent.horizontalCenter
            property real columnWidth: (width - 10) / 3
            property int rad: 10
            spacing: 5
            Rectangle{
                height: parent.height
                width: parent.columnWidth
                radius: parent.rad
                color: "lightsteelblue"
                Text{
                    anchors.centerIn: parent
                    text: form0
                    font.pointSize: del.fontSize
                }
            }
            Rectangle{
                height: parent.height
                width: parent.columnWidth
                radius: parent.rad
                color: "lightsteelblue"
                Text{
                    anchors.centerIn: parent
                    text: form1
                    font.pointSize: del.fontSize
                }
            }
            Rectangle{
                height: parent.height
                width: parent.columnWidth
                radius: parent.rad
                color: "lightsteelblue"
                Text{
                    anchors.centerIn: parent
                    text: form2
                    font.pointSize: del.fontSize
                }
            }
        }
    }
}

该模型为:

#ifndef IRREGULARLISTWRAPPER_H
#define IRREGULARLISTWRAPPER_H
#include <QObject>
#include <QList>
#include <QAbstractListModel>
#include <QMap>
#include "IrregularVerb.h"
#include "AbstractIrregularList.h"
class IrregularListWrapper : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(QString langName READ getLangName NOTIFY langChanged)
    Q_PROPERTY(int count READ rowCount NOTIFY langChanged)
    Q_ENUMS(Language)
public:
    Q_INVOKABLE int rowCount(const QModelIndex& = QModelIndex()) const { return db->count(); }
    Q_INVOKABLE QObject* get(int index) const {return db->at(index);}
    QVariant data(const QModelIndex &index, int role) const;
    enum Language
    {
        English = 0,
        German = 1
    };
    enum IrregularVerbRoles
    {
        Form0Role = Qt::UserRole + 1,
        Form1Role,
        Form2Role
    };
    IrregularListWrapper();
//    ~IrregularListWrapper() { delete db; }
//    QList<QObject*> getdb() const { return *db; }
    QString getLangName() const { return langName; }
    Q_INVOKABLE void changeLang(Language l) { beginResetModel(); db = 0; /*QList<IrregularVerb*>();*/ setLang(l); endResetModel(); }
    static QMap<Language, QString> plugins;
signals:
    void langChanged();
protected:
    void setLang(Language);
    //QList<IrregularVerb*> db;
    QString langName;
    AbstractIrregularList * db;
};
#endif // IRREGULARLISTWRAPPER_H

QMap<IrregularListWrapper::Language, QString> IrregularListWrapper::plugins;
IrregularListWrapper::IrregularListWrapper()
{
    QHash<int, QByteArray> roles;
    roles[Form0Role] = "form0";
    roles[Form1Role] = "form1";
    roles[Form2Role] = "form2";
    const QString pluginPath = "/opt/MeeIrregulars/share/lib%1.so";
    plugins[English] = pluginPath.arg("english");
    plugins[German] = pluginPath.arg("german");
    setRoleNames(roles);
    setLang(German);
}
QVariant IrregularListWrapper::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();
    const IrregularVerb* verb = db->at(index.row());
    switch (role)
    {
    case Form0Role:
        return verb->getForm0();
        break;
    case Form1Role:
        return verb->getForm1();
        break;
    case Form2Role:
        return verb->getForm2();
        break;
    }
    return QVariant();
}
void IrregularListWrapper::setLang(Language l)
{
    QPluginLoader loader(plugins[l]);
    db = qobject_cast<AbstractIrregularList*>(loader.instance());
    if (db == 0) db = new AbstractIrregularList;
    switch (l)
    {
    case English:
        langName = "English";
        break;
    case German:
        langName = "German";
        break;
    }
    emit langChanged();
}

class IrregularVerb : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString form0 READ getForm0 NOTIFY formChanged)
    Q_PROPERTY(QString form1 READ getForm1 NOTIFY formChanged)
    Q_PROPERTY(QString form2 READ getForm2 NOTIFY formChanged)
public:
    QString forms[3];
    QString getForm0() const { return forms[0]; }
    QString getForm1() const { return forms[1]; }
    QString getForm2() const { return forms[2]; }
    IrregularVerb(QString a, QString b, QString c) { forms[0] = a; forms[1] = b; forms[2] = c; }
signals:
    void formChanged();
};

回溯:

#0 QBasicAtomicInt::ref (this=0x18(
#1 QString (this=0xbe88d2a0, other=...(
#2 IrregularVerb::getForm2 (this=0x9e6de8(
#3 IrregularVerbWrapper::d ata(this=0x9e31b8, index=..., role=35(
//模型
//对libQtDeclarative的一些调用

谢谢。

这是所有权的问题。get返回的元素归JS所有并自动销毁。有关详细信息,请参阅此答案。