Qt-interest Archive, June 2007
Re: qtreeview / qtreewidget + "memory leak"
Message 1 in thread
Hello
there is 2 classes,
>
> myclass1 is inherited from QThread, it generates large QList<QStringList>
> and sends it (very often, every second and 500ms) through signal/slot
> connection to myclass2, which then updates QTreeWidget or QTreeView
> (QStandardItemModel)
>
> myclass2 is inherited from QWidget.
>
> <code>
> class myclass1 : public QThread {
> Q_OBJECT
> void run() {
> msleep(500);
> whatever();
> };
> void whatever() {
> QList<QStringList> l;
> // fill l with lots of lines
> emit signal_send(l);
> };
> signal signal_send(QList<QStringList> l);
> }
>
> class myclass2: public QWidget {
> Q_OBJECT
> public:
> // constructor, destructor etc..
> myclass2() {
> m_ui = new Ui::myui();
> m_ui->setupUi(this);
> }
> Ui::myui *m_ui; // class qt-designer +uic generated .h file
>
> public slots:
>
> void slot_receive( QList<QStringList> l) {
>
> // if we have QTreeWidget
> m_ui->treeWidget->clear();
> QTreeWidgetItem *twi;
> for(int i=0; i < l.size ; i++) {
> twi = new QTreeWidgetItem(m_ui->treeWidget);
>
> twi->setText(0, l.at(i).at(0) );
> twi->setText(1, l.at(i).at(1) );
> }
> }
>
> // if we have QTreeView then do something like this
> m_model->removeRows(0, m_model->rowsCount()); // m_model is
> QStandardItemModel
>
> QStandardItem *item;
>
> for(int i=0; i < l.size ; i++) {
> QList<QStandardItem *> qlsi;
> item = new QStandardItem( l.at(i).at(0));
> qlsi.append(item);
> item = new QStandardItem( l.at(i).at(1));
> qlsi.append(item);
>
> m_model->appendRow(qlsi);
> }
> }
> </code>
>
> memory leaks happens in myclass2::receive();
> in case of view/model if I wont clear model but rather walk through
> m_model items and update them with setText(), still it eats memory.
>
> anyway, I hope that someone more experience can spot a mistake or explain
> how to solve such task ( to update treeview/treewidget very ofter with large
> number of records).
>
>
> some notes: I am not expert in qt nor c++ so I started to investigate if
> this problem might lie in signal/slot connection of myclass1.
> When I did this in myclass2:
> void receive(QList<QStringList> l) {
> QList<QStringList> l2 = l;
> return;
> };
>
> then memory leaks disappeared. So myclass1::whatevey can process very
> large QList-st, signal slot connection can handle large QList-st but memry
> leaks happens when code starts mess with QTreeWidget/QStandardItemModel..
>
>
> thanks..
>
>
>
Message 2 in thread
On Wednesday 13 June 2007 22:53:47 sfniks sfinks wrote:
> Hello
>
>
> there is 2 classes,
>
> > myclass1 is inherited from QThread, it generates large
> > QList<QStringList> and sends it (very often, every second and 500ms)
> > through signal/slot connection to myclass2, which then updates
> > QTreeWidget or QTreeView (QStandardItemModel)
> >
> > myclass2 is inherited from QWidget.
> >
> > <code>
> > class myclass1 : public QThread {
> > Q_OBJECT
> > void run() {
> > msleep(500);
> > whatever();
> > };
> > void whatever() {
> > QList<QStringList> l;
> > // fill l with lots of lines
> > emit signal_send(l);
> > };
> > signal signal_send(QList<QStringList> l);
> > }
> >
> > class myclass2: public QWidget {
> > Q_OBJECT
> > public:
> > // constructor, destructor etc..
> > myclass2() {
> > m_ui = new Ui::myui();
> > m_ui->setupUi(this);
> > }
> > Ui::myui *m_ui; // class qt-designer +uic generated .h file
> >
> > public slots:
> >
> > void slot_receive( QList<QStringList> l) {
> >
> > // if we have QTreeWidget
> > m_ui->treeWidget->clear();
> > QTreeWidgetItem *twi;
> > for(int i=0; i < l.size ; i++) {
> > twi = new QTreeWidgetItem(m_ui->treeWidget);
> >
> > twi->setText(0, l.at(i).at(0) );
> > twi->setText(1, l.at(i).at(1) );
> > }
> > }
> >
> > // if we have QTreeView then do something like this
> > m_model->removeRows(0, m_model->rowsCount()); // m_model is
> > QStandardItemModel
> >
> > QStandardItem *item;
> >
> > for(int i=0; i < l.size ; i++) {
> > QList<QStandardItem *> qlsi;
> > item = new QStandardItem( l.at(i).at(0));
> > qlsi.append(item);
> > item = new QStandardItem( l.at(i).at(1));
> > qlsi.append(item);
> >
> > m_model->appendRow(qlsi);
> > }
> > }
> > </code>
> >
> > memory leaks happens in myclass2::receive();
> > in case of view/model if I wont clear model but rather walk through
> > m_model items and update them with setText(), still it eats memory.
> >
> > anyway, I hope that someone more experience can spot a mistake or explain
> > how to solve such task ( to update treeview/treewidget very ofter with
> > large number of records).
> >
> >
> > some notes: I am not expert in qt nor c++ so I started to investigate if
> > this problem might lie in signal/slot connection of myclass1.
> > When I did this in myclass2:
> > void receive(QList<QStringList> l) {
> > QList<QStringList> l2 = l;
> > return;
> > };
> >
> > then memory leaks disappeared. So myclass1::whatevey can process very
> > large QList-st, signal slot connection can handle large QList-st but
> > memry leaks happens when code starts mess with
> > QTreeWidget/QStandardItemModel..
> >
> >
> > thanks..
Try using valgrind on it to see what it reports.
-Benjamin Meyer
--
[ signature omitted ]
Message 3 in thread
Hi,
> [...]
> memory leaks happens in myclass2::receive();
> [...]
How do you detect these memory leaks?
--
[ signature omitted ]