Results 1 to 7 of 7

Thread: Crash while performing foreach loop: QBasicAtomicInt::ref

  1. #1
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Crash while performing foreach loop: QBasicAtomicInt::ref

    Hi

    I'm having a weird problem where my application crashes while cycling through the item in the list returned by the QObject::children() function:

    Qt Code:
    1. MyObject::function() {
    2. foreach(QObject *object, children) {
    3. doSomethigWith(object);
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    The call stack is as follows:
    #0 0x100004ad6 in QBasicAtomicInt::ref at qatomic_x86_64.h:121
    #1 0x1000506a3 in QList<QObject*>::QList at qlist.h:114
    #2 0x1000506dd in QForeachContainer<QList<QObject*> const>::QForeachContainer at qglobal.h:2227

    In my app I have a hierarchy of QObject derived classes. Also there is a model that presents this hierarchy. This crash occurs when I assign this model to the QComboBox widget.

    if I call that function MyObject::function right before the assignment, it wont crash.

    Any ideas why that might be happening?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crash while performing foreach loop: QBasicAtomicInt::ref

    Can we see a larger piece of code? Is your application multithreaded?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Crash while performing foreach loop: QBasicAtomicInt::ref

    There are background threads in the application, but this particular piece of code is run in the GUI thread. Also a different instance of the same model with the same data works just fine (the second instance of the model and view are instantiated while the first one is still active).

    A bit more of the call stack:
    #0 0x100004c36 in QBasicAtomicInt::ref at qatomic_x86_64.h:121
    #1 0x100050803 in QList<QObject*>::QList at qlist.h:114
    #2 0x10005083d in QForeachContainer<QList<QObject*> const>::QForeachContainer at qglobal.h:2227
    #3 0x100098961 in PersistenceObject:ersistenceChildren at persistenceobject.cpp:148
    #4 0x10004242c in PersistenceObject:ersistenceChildren<Patient*> at persistenceobject.h:111
    #5 0x10003d198 in Database:atients at database.cpp:106
    #6 0x1000772bc in DatabaseInformationDialog::setPatient at databaseinformationdialog.cpp:283
    #7 0x10017bece in DatabaseInformationDialog::qt_metacall at moc_databaseinformationdialog.cpp:82
    #8 0x1015c0deb in QMetaObject::activate
    #9 0x100be5910 in QComboBox::currentIndexChanged
    #10 0x100be742c in QComboBoxPrivate::_q_emitCurrentIndexChanged
    #11 0x100be8c1e in QComboBoxPrivate::setCurrentIndex
    #12 0x100be8d81 in QComboBox::setCurrentIndex
    #13 0x100beb9e6 in QComboBox::setModel
    The code does not do anything except for that loop I mentioned before the crash. Here is the more complete version: it just tries to filter the QObject children:

    Qt Code:
    1. const PersistenceObjectList PersistenceObject::persistenceChildren() const
    2. {
    3. PersistenceObjectList persistenceList;
    4. foreach (QObject *child, children()) {
    5. if (PersistenceObject *persistenceChild = qobject_cast<PersistenceObject *>(child)) {
    6. persistenceList.append(persistenceChild);
    7. }
    8. }
    9. return persistenceList;
    10. }
    To copy to clipboard, switch view to plain text mode 

    the patients function will do some more filtering of the QObject's children based on the type (using qobject_cast)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crash while performing foreach loop: QBasicAtomicInt::ref

    What is the implementation of children()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Crash while performing foreach loop: QBasicAtomicInt::ref

    it is the QObject::children() - the base implementation.

    PersistenceObject is derived from QObject

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crash while performing foreach loop: QBasicAtomicInt::ref

    Are you sure the PersistenceObject instance lives in the main thread? What does thread() return for it (interprete the value, don't post the address unless it's 0x0)?

    In general the only explanation of the situation I can think of is that two threads try to operate on your object at the same time causing havoc in the object's internals.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Crash while performing foreach loop: QBasicAtomicInt::ref

    Ok, I finally figured this out. That was due to the crappy implementation of the PIMPL pattern - it was using uninitialized pointer.

Similar Threads

  1. lifetime of foreach
    By BalaQT in forum Newbie
    Replies: 4
    Last Post: 4th March 2010, 15:55
  2. Replies: 2
    Last Post: 6th February 2010, 16:31
  3. Replies: 3
    Last Post: 12th December 2009, 08:51
  4. Foreach performance
    By jano_alex_es in forum General Programming
    Replies: 2
    Last Post: 17th November 2009, 13:26
  5. QDir entryList performing slowly
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2009, 16:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.