【发布时间】: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<...>),但抛出的异常相同。
非常感谢您的回答......
编辑
好的,解决了!
首先,就像@AndreasT 所说,我必须初始化默认的 QList 构造函数。
然后,根据@10WaRRioR01 的回答,问题来自CompareTimeChannel,它在第一次调用该方法时没有初始化。固定使用:
CompareTimeChannel* chan = static_cast<CompareTimeChannel*>(channel);
Q_ASSERT(chan);
if (chan) {
chan->setData(sections);
}
else {
qDebug() << "Dynamic cast failure";
}
谢谢大家!
【问题讨论】: