Results 1 to 17 of 17

Thread: Crash: Heap corruption due to selectedRows()

  1. #1
    Join Date
    Oct 2006
    Location
    Bangalore
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Angry Crash: Heap corruption due to selectedRows()

    Hi,

    I have a very simple thing implemented.

    //Connect statement something like this...

    connect( &contextMenu, SIGNAL( aboutToShow() ), q, SLOT( createContextMenu() ) );


    void ClientsGroupsView::createContextMenu()
    {
    if( selectionModel() == 0 )
    return;

    const QModelIndexList rows = selectionModel()->selectedRows();

    // some other code to show the contents of context menu

    }

    It is basically a tree view. When the function createContextMenu() returns ( at } ), it is causing crash stating that the crash is due to some heap corruption.

    Strangely, If I remove the line which fetches the selectedRows(), the function return does not cause any crash!. I just commented all the lines which make use of "rows" list. It still causes the crash. I do not understand this. I mean, I am not even making use of rows list ( I have to make use of it eventually though), just fetching selectionModel()->selectedRows(); causes the crash at the function return.

    Here is the call stack:

    ntdll.dll!77f22ea8()
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
    ntdll.dll!77f90c9a()
    ntdll.dll!77f64bdf()
    ntdll.dll!77ef894a()
    > QtCored4.dll!QBasicAtomicPointer<QInternal_CallBac kTable>:perator!() Line 127
    QtCored4.dll!global_callback_table() Line 2633 + 0x117 bytes C++


    Any kind of information will be very helpful.

    Thank you,
    Ankitha Varsha

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Do you set selection model somewhere in your code?

  3. #3
    Join Date
    Oct 2006
    Location
    Bangalore
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Not really, There will be a default selection model right?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Crash: Heap corruption due to selectedRows()

    yes, after you call setModel(), there will be a default selection model.

  5. #5
    Join Date
    Oct 2006
    Location
    Bangalore
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Then I am wondering what could be the problem .
    Why would the function return lead to crash if I use selectedRows() method on the selection model. I made sure that there are not illegal statements on the usage of the return value of selectedRows(). Some light please

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Probably selectedRows() returns invalid pointer. Does it crash if you only invoke selectedRows() (without dereferencing the pointer)? How do you set the model?

  7. #7
    Join Date
    Jan 2009
    Posts
    3

    Default Re: Crash: Heap corruption due to selectedRows()

    Quote Originally Posted by jacek View Post
    Probably selectedRows() returns invalid pointer. Does it crash if you only invoke selectedRows() (without dereferencing the pointer)? How do you set the model?
    Hi,

    I have the same problem.

    It does not crash when invoking selectedRows() but when the return value of selectedRows() leaves scope.

    Qt Code:
    1. QItemSelectionModel *model = myQTableView->selectionModel();
    2.  
    3. if (model)
    4. {
    5. QModelIndexList selectedRows = model->selectedRows();
    6.  
    7. if (selectedRows.count() == 1)
    8. {
    9. int selRowIdx = selectedRows[0].row() ;
    10. ...
    11. }
    12. } <---- here is the crash
    To copy to clipboard, switch view to plain text mode 

    The call stack is as follows:

    msvcr90d.dll!_CrtIsValidHeapPointer(const void * pUserData=0x0175d5c0) Line 2103 C++
    msvcr90d.dll!_free_dbg_nolock(void * pUserData=0x0175d5c0, int nBlockUse=1) Line 1317 + 0x9 bytes C++
    msvcr90d.dll!_free_dbg(void * pUserData=0x0175d5c0, int nBlockUse=1) Line 1258 + 0xd bytes C++
    msvcr90d.dll!operator delete(void * pUserData=0x0175d5c0) Line 54 + 0x10 bytes C++
    MyApp.exe!QModelIndex::`scalar deleting destructor'() + 0x46 bytes C++
    MyApp!QList<QModelIndex>::node_destruct(QList<QMod elIndex>::Node * from=0x0175d7a4, QList<QModelIndex>::Node * to=0x0175d7a4) Line 340 + 0x3e bytes C++
    MyApp!QList<QModelIndex>::free(QListData::Data * data=0x0175d790) Line 539 C++
    MyApp!QList<QModelIndex>::~QList<QModelIndex>() Line 513 C++
    Any help appreciated.
    Last edited by jacek; 30th January 2009 at 22:12. Reason: changed [code] to [quote]

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Does it crash if you comment out lines 7--11?

  9. #9
    Join Date
    Jan 2009
    Posts
    3

    Default Re: Crash: Heap corruption due to selectedRows()

    Quote Originally Posted by jacek View Post
    Does it crash if you comment out lines 7--11?
    It also chrashes if I comment out the lines 7-11.

    In the call stack you can see that the call of the destructor of QList is the problem.
    Qt Code:
    1. QModelIndexList selectedRows = model->selectedRows();
    To copy to clipboard, switch view to plain text mode 
    creates the list and after leaving scope the destructor of this list tries to delete its items.

    I think the problem is either that the item already has been delete or belongs to an other object.

    I just copied the code from the Qt-Reference and I really don't know why it crashes resp. what I could do to make it work.

  10. #10
    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: Heap corruption due to selectedRows()

    Is your application multithreaded?

  11. #11
    Join Date
    Jan 2009
    Posts
    3

    Default Re: Crash: Heap corruption due to selectedRows()

    Quote Originally Posted by wysota View Post
    Is your application multithreaded?
    No it is not

    Here is an example:
    test.h
    Qt Code:
    1. #ifndef TEST_H_
    2. #define TEST_H_
    3.  
    4. #include "types.h"
    5.  
    6. #include "Ui_test.h"
    7.  
    8.  
    9. class tableView : public QDialog, protected Ui_Dialog
    10. {
    11. Q_OBJECT
    12. public:
    13. tableView(QWidget* parent = 0, Qt::WindowFlags flags = 0);
    14.  
    15. private slots:
    16. void btn_testClick();
    17. };
    18.  
    19. #endif // end of #ifndef TEST_H_
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include <QtGui/QMessageBox>
    2. #include <QtGui/QStandardItemModel>
    3. #include <QtGui/QItemSelectionModel>
    4.  
    5. #include "test.h"
    6.  
    7.  
    8. tableView::tableView(QWidget* parent /* = 0 */, Qt::WindowFlags flags /* = 0 */)
    9. : QDialog(parent, flags)
    10. {
    11. // create gui elements defined in the Ui_tableView class
    12. setupUi(this);
    13.  
    14. connect(btn_test, SIGNAL(clicked()), this, SLOT(btn_testClick()) );
    15.  
    16. setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    17.  
    18. if (!tv_test->model())
    19. {
    20. QStandardItemModel *newModel = new QStandardItemModel(0, 1);
    21. tv_test->setModel(newModel);
    22. }
    23.  
    24. QStandardItemModel *model = dynamic_cast<QStandardItemModel*>( tv_test->model() );
    25.  
    26. for (int idx = 1; idx < 11; idx++)
    27. {
    28. QStandardItem* item1 = new QStandardItem( QString("bla_%1").arg(idx) );
    29. model->appendRow( item1 );
    30. }
    31.  
    32. tv_test->setSelectionModel( new QItemSelectionModel( model ) );
    33. }
    34.  
    35. void tableView::btn_testClick()
    36. {
    37. QItemSelectionModel *model = tv_test->selectionModel();
    38.  
    39. if (model)
    40. {
    41. QModelIndexList selectedRows = model->selectedRows();
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    Remark: The code just crashes if I select a row and push the Test button.
    Last edited by Yeter; 3rd February 2009 at 11:24.

  12. #12
    Join Date
    Sep 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Hi, please try following code it may solve the problem.
    this code is to get selectedRows

    Qt Code:
    1. QModelIndexList getSelectedRows()
    2. {
    3. QModelIndexList lstIndex ;
    4.  
    5. QItemSelection ranges = selectionModel()->selection();
    6. for (int i = 0; i < ranges.count(); ++i)
    7. {
    8. QModelIndex parent = ranges.at(i).parent();
    9. int right = ranges.at(i).model()->columnCount(parent) - 1;
    10. if (ranges.at(i).left() == 0 && ranges.at(i).right() == right)
    11. for (int r = ranges.at(i).top(); r <= ranges.at(i).bottom(); ++r)
    12. lstIndex.append(ranges.at(i).model()->index(r, 0, parent));
    13. }
    14. return lstIndex;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st October 2009 at 08:10. Reason: missing [code] tags

  13. #13
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    I have the similar problem. Application crashes when it go out from the scope. I put a code sample below. Application will crash even if I remove strings 3 & 9-11. It crushes when trying to destruct QList object

    Qt Code:
    1. void SpreadsheetView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    2. {
    3. QItemSelectionModel* sModel = selectionModel();
    4.  
    5. QModelIndexList selectedItems;
    6. selectedItems.append(sModel->selectedIndexes());
    7.  
    8.  
    9. QModelIndex sel = deselected.indexes().value(0);
    10. if(!deselected.indexes().empty() && selectedItems.count() > 0)
    11. sModel->setCurrentIndex(selectedItems[selectedItems.count() - 1], QItemSelectionModel::Select);
    12.  
    13. QTreeView::selectionChanged(selected, deselected);
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Quote Originally Posted by andrey_s View Post
    I have the similar problem. Application crashes when it go out from the scope. I put a code sample below. Application will crash even if I remove strings 3 & 9-11. It crushes when trying to destruct QList object
    I've found solution of my problem. There was 2 debug dll files included into the link section of project settings(VS2005) together with release libs.

    I've spent 3(!) days to found out this. I so tired

  15. #15
    Join Date
    Feb 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    I have the same broblem. Do yoy remember what you exactly did?

  16. #16
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    Advice: use "PageHeap" or "Application Verifier".
    It will point out heap corruptions.

    Cheers

  17. #17
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Crash: Heap corruption due to selectedRows()

    I'm having this same problem now as well. I've verified that my Debug build is using all debug DLL's and Release is using all release DLL's. However in both states I'm getting a crash from this the same way that the code snippit providers above do.

    Is there something I can do to try and force this?

    The Qt Devs say to check which DLLs you are using as well, which doesn't help me:
    http://bugreports.qt.nokia.com/browse/QTBUG-7884

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.