[英]segfault accessing qlist element through an iterator
I get a segfault while iterating over a QList. 在遍历QList时遇到段错误。 I don't understand what I am doing wrong. 我不明白我在做什么错。
I have a QList of Conversation. 我有一个会话QList。 Inside a Conversation I have a QList of Msg. 在对话中,我有一个Msg的QList。 Below are the class description : 下面是类说明:
Msg class : 讯息类别:
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
};
Conversation class : 会话课程:
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);
}
Code that generate the segfault. 生成段错误的代码。 I create a message, I iterate over the Conversation list to add the message in the related conversation. 我创建一条消息,然后遍历“对话”列表以将该消息添加到相关对话中。 Then, i want to iterate over the message list and I get a segfault. 然后,我想遍历消息列表,然后遇到段错误。 I use different way to access to the message which works fine. 我使用其他方式来访问正常工作的消息。
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.
}
Thank you for your help 谢谢您的帮助
(Edited away first "answer", this is an actual attempt at an answer) (已删除第一个“答案”,这是对答案的实际尝试)
My guess: 我猜:
QList<Msg> messages() const { return _messages; }
It's returning a copy of the QList _messages, rather than a reference to it. 它返回QList _messages的副本,而不是对其的引用。 I'm not sure that it would give the results you're seeing, but it looks wrong to me. 我不确定它能否提供您所看到的结果,但对我来说似乎是错误的。 Maybe try something like this? 也许尝试这样的事情?
QList<Msg>& messages() const { return _messages; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.