Results 1 to 9 of 9

Thread: ASSERT Failure in QAbstractItemModel Pure Virtual Function

  1. #1
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default ASSERT Failure in QAbstractItemModel Pure Virtual Function

    I’m pulling my hair out on this one and it’s probably something simple, but I can’t find it. I initially sub-classed QAbstractItemModel and everything seemed to work fine. I made what appeared to be unrelated changes in a different .cpp file when the program started reporting an ASSERT error. I believe I changed everything back, to the state it was in when it was working properly and yet it still reported the same error.

    Using the debugger, I stepped through the program and found the location where the problem was starting. Surprisingly it was transpiring in the pure virtual function “data” at the line where I was checking if the request was referencing the TextAlignmentRole - line 116 in the code listing.

    Qt Code:
    1. 114 QVariant RDDSModel::data(const QModelIndex &index, int role) const
    2. 115 {
    3. 116 if (role == Qt::TextAlignmentRole)
    4.  
    5. 117 return Qt::AlignCenter;
    6. 118
    7. 119 ModelItem* pItem;
    8. 120 if(role == Qt::DisplayRole){
    9. 121 if(index.isValid())
    10. 122 pItem = static_cast<ModelItem*>(index.internalPointer());
    11. 123 else
    12. 124 pItem = pRoot;
    13. 125 return pItem->getData(index.column());
    14. 126 }
    15. 127 return QVariant();
    16. 128 }
    To copy to clipboard, switch view to plain text mode 

    The actual error message received is...

    ASSERT failure in QVector<T>::at: "index out of range"

    The debugger indicates that the error is being reported from qvector.h at line 429. Code shown...

    Qt Code:
    1. 427 template <typename T>
    2. 428 inline const T &QVector<T>::at(int i) const
    3. 429 { Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
    4. 430 return d->begin()[i]; }
    To copy to clipboard, switch view to plain text mode 

    At this time, I fail to seen any connection to the error message and the code where the error originated. Any help on this issue would be highly appreciated.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Errors of this sort are invariably a red herring. What has likely happened is that your program has overwritten memory by writing off the end of an array or other local data structure, and that has corrupted the stack. When the stack gets corrupted, the stack pointer can start executing code in a totally unrelated part of your program on the return from the function, so you get errors like this that have nothing to do with the real problem.

    The solution is to run your program in the debugger and try to reproduce the error. When it happens, look at the stack traceback until you see a call to a function that makes sense given what you were doing to reproduce the problem. Look there (and in calls preceeding it) for the place where the stack corruption could have occurred.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Thank you very much your prompt reply. I don’t know if it’s important, but I think I should point out that when you say I should “try” and reproduce the error, that the error is actually quite consistent, and reproducing it is not an issue. But I did as you suggested and I’m unable to identify any particular problem area. I see the program start, all the Qt calls to paint the window and buttons, and then the mouse handling calls for the button push which leads to the function that is giving the problem. At no point in that chain is there anything that looks odd (not sure I would recognize it if there was) except for just before receiving the error, at lines 78 and 79, there are questions marks and empty brackets. I have no idea whether that is abnormal or not. I’m providing the last part of the backtrace. lines 71 - 79 (following the mouse event). Nothing prior to the mouse event appears anomalous.

    Qt Code:
    1. #71 0x00de60f4 in QGuiApplicationPrivate::processMouseEvent (e=0x17dbff58) at kernel\qguiapplication.cpp:1927
    2. type = QEvent::MouseButtonRelease
    3. stateChange = {i = 1}
    4. window = 0x17d31ac0
    5. localPoint = {xp = 50, yp = 443}
    6. globalPoint = {xp = 2155, yp = 718}
    7. button = Qt::LeftButton
    8. doubleClick = false
    9. frameStrut = false
    10. ev = {<QInputEvent> = {<QEvent> = {_vptr.QEvent = 0x12a0418 <vtable for QMouseEvent+8>, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6be6ba00 <qt_meta_stringdata_QEvent>, data = 0x6be6cee0 <qt_meta_data_QEvent>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}, d = 0x0, t = 3, posted = 0, spont = 1, m_accept = 1, reserved = 0}, modState = {i = 0}, ts = 2849}, l = {xp = 50, yp = 443}, w = {xp = 50, yp = 443}, s = {xp = 2155, yp = 718}, b = Qt::LeftButton, mouseState = {i = 0}, caps = 0, velocity = {xp = 0, yp = 0}}
    11.  
    12. #72 0x00de5678 in QGuiApplicationPrivate::processWindowSystemEvent (e=0x17dbff58) at kernel\qguiapplication.cpp:1711
    13. __PRETTY_FUNCTION__ = "static void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*)"
    14.  
    15. #73 0x00dd75d6 in QWindowSystemInterface::sendWindowSystemEvents (flags=...) at kernel\qwindowsysteminterface.cpp:710
    16. event = 0x17dbff58
    17. nevents = 1
    18.  
    19. #74 0x62a843c9 in QWindowsGuiEventDispatcher::sendPostedEvents (this=0x3e9618) at qwindowsguieventdispatcher.cpp:82
    20. No locals.
    21.  
    22. #75 0x6bb2a726 in qt_internal_proc(HWND__*, unsigned int, unsigned int, long)@16 (hwnd=0x3805fa <midiStreamPosition+2>, message=1025, wp=0, lp=0) at kernel\qeventdispatcher_win.cpp:221
    23. localSerialNumber = 201
    24. msg = {hwnd = 0x3805fa <midiStreamPosition+2>, message = 1025, wParam = 0, lParam = 0, time = 2677944, pt = {x = 1806871059, y = 0}}
    25. dispatcher = 0x3e9618
    26. result = 0
    27. q = 0x3e9618
    28. d = 0x17d01a68
    29.  
    30. #76 0x761762fa in gapfnScSendMessage () from C:\Windows\syswow64\user32.dll
    31. No symbol table info available.
    32.  
    33. #77 0x003805fa in midiStreamPosition () from C:\Windows\system32\winmm.dll
    34. No symbol table info available.
    35.  
    36. #78 0x00000401 in ?? ()
    37. No symbol table info available.
    38.  
    39. #79 0x00000000 in ?? ()
    40. No symbol table info available.
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Well then, if it is the mouse button press that seems to trigger the bad behavior, I would start by putting a breakpoint at the start of that slot and stepping through the code line by line (including stepping into any functions called from within the slot) to see if there is something funny going on.

    Another test would be to -not- try to reproduce the problem, but to just use whatever you can of the program -without- clicking the button. If at some point it breaks, then it would indicate that the button click is also a red herring and that the corruption is occurring earlier or somewhere else.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    Corny (23rd May 2018)

  6. #5
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Thanks again for your assistance. I'll give what you suggest a try.

  7. #6
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Sine you are lucky and Qt tells you exactly where you went wrong, you also breakpoint the QVector::at function shorty before the error occurs and backtrack from there, with the stack intact.

  8. #7
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Qt Code:
    1. 121 if(index.isValid())
    2. 122 pItem = static_cast<ModelItem*>(index.internalPointer());
    3. 123 else
    4. 124 pItem = pRoot;
    5. 125 return pItem->getData(index.column());
    To copy to clipboard, switch view to plain text mode 
    What would you expect here instead of an assertion when index is not valid? Hint: when index is not valid, index.column() will not contain a valid column but -1...

  9. #8
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Quote Originally Posted by ChristianEhrlicher View Post
    Qt Code:
    1. 121 if(index.isValid())
    2. 122 pItem = static_cast<ModelItem*>(index.internalPointer());
    3. 123 else
    4. 124 pItem = pRoot;
    5. 125 return pItem->getData(index.column());
    To copy to clipboard, switch view to plain text mode 
    What would you expect here instead of an assertion when index is not valid? Hint: when index is not valid, index.column() will not contain a valid column but -1...
    Thanks! That pointed out the error of my ways. It seems so obvious now.
    Thanks again.

  10. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ASSERT Failure in QAbstractItemModel Pure Virtual Function

    Yes, I did a dope slap on my head as well. Sorry for sending you on a wild goose chase when the problem was staring us in the face. Sometimes no matter how carefully you read code, the obvious escapes you.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 11
    Last Post: 14th August 2014, 10:08
  2. Assert failure with tablet installed
    By Spectralist in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2011, 14:31
  3. Problem debugging Assert failure.
    By Doug Broadwell in forum Qt Programming
    Replies: 3
    Last Post: 3rd January 2009, 19:39
  4. Debugging ASSERT failure in Qt header.
    By Doug Broadwell in forum Qt Programming
    Replies: 6
    Last Post: 27th December 2008, 02:40
  5. QWT assert failure
    By Andimat in forum Qwt
    Replies: 1
    Last Post: 21st June 2007, 15:36

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.