[SOLVED] my code make my memory increasing
-
How to correct my code?
void diaOpenFileByListView::BindLV(){ int curRow=-1; if(model!=0 && ui->lvwFile->currentIndex().row()>0){ curRow= ui->lvwFile->currentIndex().row(); } init_styleditemdelegate_cell *listdelegate; model = new QStandardItemModel(this); ui->lvwFile->setModel(model); listdelegate = new init_styleditemdelegate_cell(); ui->lvwFile->setItemDelegate(listdelegate); QDir dir=QDir::rootPath(); QString myPath; myPath=gData->strFileFolder; dir.cd(myPath); ui->lblPath->setText(dir.absolutePath()); dir.setNameFilters(QStringList()<<"*.qqq"); QStringList fileList = dir.entryList(); QStandardItem *item ;//= new QStandardItem(); for (int i=0; i<fileList.count(); i++) { item = new QStandardItem(); QString tmpFilePaht=dir.absolutePath()+"/"+fileList[i]; QIcon icon=GetListIcon(tmpFilePaht); item->setData(icon,init_styleditemdelegate_cell::IconRole); item->setData(tmpFilePaht,init_styleditemdelegate_cell::headerTextRole); item->setData(fileList[i],init_styleditemdelegate_cell::subHeaderTextrole); item->setEditable(false); model->appendRow(item); } if(model->rowCount()>0){ if(curRow>-1){ ui->lvwFile->setCurrentIndex(model->indexFromItem(model->item(curRow,0))); }else{ ui->lvwFile->setCurrentIndex(model->indexFromItem(model->item(0,0))); } } ShowIni(); }
-
This post is deleted! -
The only leak I can find is caused by
ui->lvwFile->setModel(model)
. setModel(model) does not free the old model, instead you need to delete it manually. Your coding style could use some fine tuning init_styleditemdelegate_cell is absolutely horrible class name, and you could add a few newlines here and there to group logical pieces together. -
@Huulivoide said in [SOLVED] my code make my memory increasing:
setModel
How can i delete the model manually in the code? Please guide.
is there any function which I can use to delete it.
I found model->deleteLater(); but not sure if this is the correct way to delete the model object manually. -
@qtwithanzo hi,
If the model might still be connected elsewhere then yes it's the correct way. Otherwise use the standard C++ delete.