views:

43

answers:

1

Hello,

(Disclaimer: I have removed the Qt tag in case the problem is in my syntax / understanding of the references involved here)

I have a foreach loop with an object Member. When I enumerate through the list and try to access a member field, the debugger stops and I get a message:

Stopped: 'signal-received' -

The assert failure is:

inline QString::QString(const QString &other) : d(other.d)
{ Q_ASSERT(&other != this); d->ref.ref(); }

I have checked if the member is NULL, and it isn't. I have tried re-working the code, but I keep failing on this simple call.

Some thing's I missed out. MemberList is a singleton (definitely initialized and returns a valid pointer) that is created as the application launches and populates the MemberList with Members from a file. When this is created, there are definitely values, as I print them to qDebug(). This page is literally the next page. I am unsure as to how the List items can be destroyed.

The code is as follows:

int i = 0;

QList<Member*> members = ml->getMembers();

foreach (Member* mem, members)
{
    QString memID = mem->getMemberID(); // Crash happens here

    QListWidgetItem *lstItem = new QListWidgetItem(memID, lsvMembers);
    lsvMembers->insertItem(i, lstItem);
    i++;
}   

The Member classes get is as follows:

QString getMemberID() const;

and the actual function is:

QString Member::getMemberID() const
{
    return MemberID;
}

The ml variable is received as follows:

QList<Member*> MemberList::getMembers()
{
    return MemberList::getInstance()->memberList;
}

Where memberList is a private variable.


Final answer:

I decided to rework the singleton completely and found that I was not instantiating a new Member, rather reusing the previous object over and over. This caused the double reference. S'pose thats pointers for you. Special thanks to Troubadour for the effort!

A: 

If mem is not null it could still be the case that the pointer is dangling i.e. the Member it was pointing to has been deleted.

If Member inherits from QObject then you could temporarily change your QList<Member*> that is stored in ml (assuming that's what's stored in ml) into a QList< QPointer<Member> >. If you then get a null QPointer in the list after calling getMembers or at any point during the loop then the object must have been destroyed at some point.

Edit

As regards the singleton, are you sure it's initiliased properly? In other words does MemberList::getInstance() return a valid pointer or just a random uninitialised one?

Edit2

Since we've exhausted most possibilities I guess it must be in the singleton somewhere. All I can suggest is to keep querying the first item in the list to find out exactly where it goes bad.

Troubadour
Thanks - I updated the question with a new paragraph. Would this not stop the items from being `null` due to the MemberList being a singleton?
Qt Runner
@QtRunner: Hmm, interesting. It does sound unlikely that they would have been destroyed then. After you create each one individually have you tried immediately calling `getMemberID` on it?
Troubadour
Well, as they're created and added to the list, I have a `toString()` method that I call. The objects are printed out perfectly. I am so stumped.
Qt Runner
@QtRunner: See the edit to my answer. Are you really getting the singleton back from `getInstance`?
Troubadour
The singleton definitely returns a valid pointer. I've checked this immediately before the call that crashes.
Qt Runner