【问题标题】:Feed a QList with another用另一个 QList 喂食
【发布时间】:2013-03-06 13:37:57
【问题描述】:

您好,我正在尝试将 QList 作为参数发送到另一个类,但由于某种原因,我遇到了读取访问冲突...

CompareTimeChannel.h

 class CompareTimeChannel : public IDataChannel
   public:

      // @brief The method used to receive the list
      void setData(const QList< QSharedPointer<core::ITrackSection> > & sections);

      // @brief The list
      QList< QSharedPointer<core::ITrackSection> > _sections;

 };

CompareTimeChannel.cpp

 // @brief Empty constructor
 CompareTimeChannel::CompareTimeChannel()
 {
 }

 void CompareTimeChannel::setData(const QList< QSharedPointer<core::ITrackSection> > & sections)
 {
     //_sections = *new QList< QSharedPointer<core::ITrackSection> > ();
     _sections.clear();
     _sections.append(sections);
 }

运行此代码将在_sections.clear(); 上抛出Exception at 0x31cc78d, code: 0xc0000005: read access violation at: 0x4, flags=0x0

我之前尝试初始化列表(注释行_sections = *new QList&lt;...&gt;),但抛出的异常相同。

非常感谢您的回答......

编辑

好的,解决了!

首先,就像@AndreasT 所说,我必须初始化默认的 QList 构造函数。

然后,根据@10WaRRioR01 的回答,问题来自CompareTimeChannel,它在第一次调用该方法时没有初始化。固定使用:

        CompareTimeChannel* chan = static_cast<CompareTimeChannel*>(channel);
        Q_ASSERT(chan);
        if (chan) {
            chan->setData(sections);
        }
        else {
            qDebug() << "Dynamic cast failure";
        }

谢谢大家!

【问题讨论】:

    标签: c++ qt qlist


    【解决方案1】:
    //_sections = *new QList< QSharedPointer<core::ITrackSection> > ();
    

    你永远不应该做这样的事情。这会在堆上创建一个新的 QList 实例,它永远不会被删除,所以你有内存泄漏
    你应该做的

    _sections = QList< QSharedPointer<core::ITrackSection> > ();
    

    相反,这将是合法的。但最简单的方法是使用这样的复制分配

    _sections = sections
    

    您遇到的问题很可能与您在_sections 中的数据有关。也许你在一个空的 CompareTimeChannel 对象上调用你的方法

    【讨论】:

    • "您遇到的问题很可能与您在 _sections 中的数据有关。" -> 你的意思是问题应该来自core::ITrackSection
    • 是的,我想是的。其余的代码看起来不错。也许您缺少虚拟析构函数或其他东西
    • 在这种情况下,如果_sectionssection 列表都是空的,会不会出现这个问题?
    • 没有。但是,如果您甚至无法访问列表的大小,则应检查是否在 null 对象上调用 setData()
    • 好的...所以,有两个问题:1)我可以检查传递给setData()的对象是否是null? 2)我无法访问CompareTimeChannel.h中声明的_sections类成员的大小,它与setData()的参数有关?
    【解决方案2】:

    您应该在构造函数中初始化部分。 注释行是非常错误的。

    new 在堆上构造 List,然后使用 *new 取消引用它,并且赋值隐式调用堆上新列表的复制构造函数并将其复制到实例中。不过,堆上的东西仍然存在,所以您只是造成了内存泄漏。

    // @brief Empty constructor
     CompareTimeChannel::CompareTimeChannel()
    :_sections()   // initialization default constructor.
     {
     }
    

    修改评论:

    QList.clear() 方法调用列表中每个元素的析构函数。至少您的一个共享指针似乎没有正确初始化。如果您需要更多信息,请粘贴将内容放入 _sections 的代码。

    编辑关于例外: 正如我所说,问题很可能是共享指针没有设置为任何有趣的东西。当 SP 被销毁时,它会调用其内容的析构函数,该析构函数必须存在,否则会引发读取访问冲突,这将解释症状。

    【讨论】:

    • "注释行是非常错误的。"啊哈 :) 但是我得到了同样的错误,但是在_sections.append(sections); now => 0x300c78d 处的异常,代码:0xc0000005:读取访问冲突:0x4,标志=0x0
    • 好的...但是,根据 QList 文档,我不想破坏列表中的所有对象,只需从列表中删除项目。提供部分的代码(在此类中命名为 _sec): void GraphModel::addSection(const QSharedPointer<:itracksection> & section) { _mutex.lock(); _selectionUpdated = true; _sec
    • 我明白了,但我不明白为什么 SharedPointers 会被破坏......或者QList::clear 不是我需要的方法......现在,让我用:@ 987654327@ 我得到同样的错误,只是通过阅读......
    【解决方案3】:

    这你展示的应该可以工作。您的问题出在其他地方。
    此类问题可能是由许多不同的错误引起的,例如:bad static_cast 或错误的 c 风格转换、使用动态库时二进制兼容性中断、表外写入、编译器缓存问题(这种情况发生通常如此治愈的方法如下)。

    首先我会尝试做什么:

    make clean
    qmake
    make
    

    这经常解决此类问题。如果我没有帮助您,您必须在您的代码中找到其他问题。

    【讨论】:

    • 我会试试的。我不知道它是否相关,但是 setData() 方法是这样调用的:dynamic_cast&lt;CompareTimeChannel*&gt;(channel)-&gt;setData(sections);
    猜你喜欢
    • 2021-07-09
    • 2021-05-12
    • 1970-01-01
    • 2015-02-07
    • 2017-03-21
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多