Results 1 to 8 of 8

Thread: Garbage collector issue QAbstractListModel

  1. #1
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Garbage collector issue QAbstractListModel

    Hi ,
    i'm working with qt 4.8.

    I have my model in c++ and a ListView in Qml.
    I set my model in c++ like this
    Qt Code:
    1. ListModel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. enum ListModelRole
    7. {
    8. rName = Qt::UserRole + 1,
    9. rId = Qt::UserRole + 1,
    10. };
    11. ListModel (QObject *parent = 0);
    12. virtual ~ListModel ();
    13.  
    14. virtual void ClearWorklistModel();
    15. int rowCount(const QModelIndex & parent = QModelIndex()) const;
    16. QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    17. void SetValues( const vector<pair<string, string>> & iValues );
    18.  
    19. protected :
    20. vector<pair<string, string>> m_values ;
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ListModel::ListModel( QObject *pParent/*NULL*/ )
    2. {
    3. QHash<int, QByteArray> roles;
    4. roles[rName ] = "rName";
    5. roles[rId ] = "rId";
    6. setRoleNames( roles );
    7. }
    8. int
    9. ListModel::rowCount( const QModelIndex & parent ) const
    10. {
    11. return m_values.size();
    12. }
    13. void
    14. ListModel::ClearWorklistModel()
    15. {
    16. beginRemoveRows( startRefreshIndex, 0, m_values.size()-1 );
    17. this->removeRows( 0, m_values.size()-1 );
    18. endRemoveRows();
    19. m_values.clear();
    20. }
    21.  
    22. void
    23. ListModel::SetValues( const vector<pair<string, string>> & iValues )
    24. {
    25. this->ClearWorklistModel();
    26. this->beginResetModel();
    27. m_values = iValues ;
    28. this->endResetModel();
    29. }
    30.  
    31. ListModel::data( const QModelIndex & index, int role ) const
    32. {
    33. // check conditions
    34. if (index.row() < 0 || (size_t)index.row() > m_values.size() )
    35. {
    36. return QVariant();
    37. }
    38. QVariant value ;
    39. switch( role )
    40. {
    41. case rName :
    42. value = m_values.at( index.row() ).first.c_str() ;
    43. break;
    44. case rId :
    45. value = m_values.at( index.row() ).second.c_str() ;
    46. break;
    47. }
    48. return value ;
    49. }
    To copy to clipboard, switch view to plain text mode 
    and my ListView

    Qt Code:
    1. ListView
    2. {
    3. id : myListView
    4. objectName : "myWorklistView"
    5.  
    6. model: myModel
    7. delegate: Item
    8. {
    9. Text
    10. {
    11. test : rName + " _ " + rId
    12. }
    13. Image
    14. {
    15. source: "ClientThumbnail.png"
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    So in my program i create a QDeclarativeView and then i set the model like

    Qt Code:
    1. QDeclarativeView *pDeclarativeListView = new QDeclarativeView( QUrl("myqmlFile.qml") );
    2. QDeclarativeContext * pDeclarativeCtxt = pDeclarativeListView->rootContext();
    3. pDeclarativeCtxt->setContextProperty( "myModel", &currentWorkListModel );
    To copy to clipboard, switch view to plain text mode 

    At last i want to refresh my ListView i call SetValues but each time i set new Values i have some memory consumption. I notice when i delete my DeclarativeView the memory consumption is fine.

    I imagine there is some Garbage Collector in QML that handle QDeclarativeItems created but how can I tell to QML that i want that items which are no longer available in my ListView have to be deleted.

    Thanks in advance

    Pierre

  2. #2
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Garbage collector issue QAbstractListModel

    Ok i found how to say to delete the clean up items. I have to tell to the QApplication to Delete Deferred Items so i send post message to the app.

  3. #3
    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: Garbage collector issue QAbstractListModel

    The items will be deleted automatically when the time comes, no need to do anything.
    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.


  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Garbage collector issue QAbstractListModel

    also: your SetValues needless calls ClearWorkListModel(), it resets anyway.

    Your ClearWorkListModel() could also just do a reset.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Garbage collector issue QAbstractListModel

    Ok,
    but the issue was that i could fill up my model for instance an hundread time, and then the program crash because items were not deleted and program can't allocate more memory. With calling this QCoreApplication::sendPostedEvents(0, QEvent:eferredDelete);, i do not have this issue anymore. My memory is fine.

    Thank you for your time

  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: Garbage collector issue QAbstractListModel

    Deferred delete happens automatically whenever the application processes its events so there should be no need for such calls. If you do require it, then it means something related to event processing in your application is seriously broken. Are you calling QApplication::exec() in your main function?
    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
    Sep 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Garbage collector issue QAbstractListModel

    Effectively the event processing seems to be broken... Because in some case my items are well deleted. Exec is effectively called.

  8. #8
    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: Garbage collector issue QAbstractListModel

    Are you blocking the event loop in any way? Do you have some long running while or for loop somewhere in your code related to using the model?
    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.


Similar Threads

  1. Qt closeevent and garbage collection
    By ehnuh in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2012, 16:36
  2. QList and garbage collection
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2012, 14:38
  3. Garbage collector - PyQt
    By janosimas in forum Newbie
    Replies: 0
    Last Post: 11th December 2011, 11:05
  4. Garbage collection
    By Septi in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 14:13
  5. question about garbage collection
    By Dumbledore in forum Qt Programming
    Replies: 4
    Last Post: 18th December 2007, 22:08

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.