【发布时间】:2015-06-22 21:15:58
【问题描述】:
迭代 QList 时出现段错误。我不明白我做错了什么。
我有一个 QList of Conversation。在对话中,我有一个 Msg 的 QList。以下是课程说明:
消息类:
class Msg {
public:
Msg();
Msg(const Msg& other);
Msg& operator=(const Msg& other);
virtual ~Msg();
bool operator==(const Msg& other);
QString id() const { return _id; }
MsgContact author() const { return _author; }
MsgContact dest() const { return _dest; }
QDateTime date() const { return _receivedDate; }
MsgDirection direction() const { return _direction; }
QString text() const { return _text; }
bool transmitted() const { return _transmitted; }
void setId(const QString& id) { _id = id; }
void setAuthor(const MsgContact& author) { _author = author; }
void setDest(const MsgContact& dest) { _dest = dest; }
void setDate(const QDateTime& receivedDate) { _receivedDate = receivedDate; }
void setDirection(const MsgDirection& direction) { _direction = direction; }
void setText(const QString& text) { _text = text; }
void setTransmitted(const bool& transmitted) { _transmitted = transmitted; }
private:
QString _id;
MsgContact _author;
MsgContact _dest;
QDateTime _receivedDate;
MsgDirection _direction;
QString _text;
bool _transmitted; //indique que le message a été transmis
bool _read; //indique la lecture
};
会话课:
class Conversation
{
public:
Conversation();
Conversation(const Conversation& other);
virtual ~Conversation();
Conversation& operator=(const Conversation& other);
bool operator==(const Conversation& other);
bool isNull() const { return (NULL == _title || NULL == _destId); }
const QString title() const { return _title; }
const QString destId() const { return _destId; }
QList<Msg> messages() const { return _messages; }
void setDestId(const QString& destId) { _destId = destId; }
void setTitle(const QString& title) { _title = title; }
void addMsg(const Msg& msg);
static Conversation INVALID_CONVERSATION;
private:
QList<Msg> _messages;
QString _title;
QString _destId;
};
void Conversation::addMsg(const Msg& msg)
{
_messages.append(msg);
}
生成段错误的代码。我创建一条消息,遍历对话列表以在相关对话中添加消息。然后,我想遍历消息列表,我得到一个段错误。我使用不同的方式来访问可以正常工作的消息。
Msg *m = new Msg();
m->setId(xmppMsg.id());
m->setDest(findContactById(conversationId));
m->setDirection(MsgOutgoing);
m->setAuthor(_myContact);
m->setText(message);
m->setDate(xmppMsg.stamp());
QList<Conversation>::iterator it;
for(it = _conversations.begin(); _conversations.end() != it; it++)
{
if((*it).destId() == conversationId)
{
(*it).addMsg(*m);
Q_EMIT(conversationChanged((*it)));
break;
}
}
qDebug() << "NB : " <<(*it).messages().size(); // ok, the number of message is incremented.
//test several way of accessing a message, these works fine.
qDebug() << "doSend " << it->messages().at(0).id();
qDebug() << "doSend " << it->messages().begin()->id();
qDebug() << "doSend " << (*(it->messages().begin())).id();
//try to iterate
QList<Msg>::iterator msgIt = it->messages().begin();
if(msgIt != it->messages().end())
{
qDebug() << "TEST - "<< msgIt->id(); //segfault.
}
感谢您的帮助
【问题讨论】:
-
嗯,你在调试器中单步调试代码了吗?
-
@OldProgrammer 看起来像,因为他们似乎知道段错误出现的确切位置。从问题来看,它看起来有点可疑,因为
qDebug() << "doSend " << it->messages().begin()->id();似乎工作正常。 -
@OldProgrammer 是的,我使用了调试器。我检查了消息是否正确插入到我的列表中。似乎是因为messge的ID显示正确。但是,当使用迭代器时它不起作用。我在调试器中看到迭代器指向 0x0 (NULL)