segfault accessing qlist element through an iterator

346 views Asked by At

I get a segfault while iterating over a QList. I don't understand what I am doing wrong.

I have a QList of Conversation. Inside a Conversation I have a QList of Msg. 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

1

There are 1 answers

0
Doug Porter On BEST ANSWER

(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. 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; }