PDA

View Full Version : Removing objects from memory - necessary?



Tomasz
26th August 2010, 13:05
Hello!

I've got function in which I've got something like that:



void MainWindow::myFunction()
{
[...]

bool ok;
QTextStream out(stdout);
QSqlRecord rec;

QSqlDatabase bdb = QSqlDatabase::addDatabase("QSQLITE");

bdb.setDatabaseName(databaseName);
ok = bdb.open();

if (ok)
{
out << endl << "Otworzylem baze!" << endl ;
} else {
out << "Nie udalo sie otworzyc bazy!" << endl;
}

QSqlQueryModel *queryModel = new QSqlQueryModel;

queryModel->setQuery("SELECT * FROM config WHERE name='qt-notification-interval'", bdb);

rec = queryModel->record(0);
notificationInterval = (rec.value(1).toInt())*1000;

out << notificationInterval << endl;

bdb.close();

[...]
}


I invoke this function many times. Is it necessary to remove somehow from memory queryModel? Or is it removed automatically after exiting function?

thanks in advance
best regards
Tomasz

Lykurg
26th August 2010, 13:48
Create the query on the stack then you don't have to take care about. (and use a simple QSqlQuery if you don't have strong reasons using the model.)
QSqlQueryModel queryModel;So it gets deleted after the scope ends.

EDIT: if you don't delete it yourself you create memory leaks and your applications memory need becomes big with the time.

Tomasz
26th August 2010, 14:11
EDIT: if you don't delete it yourself you create memory leaks and your applications memory need becomes big with the time.

I've forgot about memory leaks, and now my application is growing really fast, and sometimes variables changes their values (is it possible?!). I think thats the reason. I'll do as You said - without pointer.

But if i really need to create object with pointer - how delete it?

thanks in advance
best regards
Tomasz

Lykurg
26th August 2010, 14:47
But if i really need to create object with pointer - how delete it? When do not need the pointer anymore you can do it the old C++ way using:
QWidget *w = new QWidget;
delete w;
If it is QObject, or any class that has a QObject as base you can alos do:
QWidget *w = new QWidget;
w.deleteLater(); See QObject::deleteLater().

Tomasz
26th August 2010, 22:46
I've tried delete on some widgets:



QMessageBox *msgBox=new QMessageBox;

if(rec.value(1).toString()=="warning") msgBox->setIcon(QMessageBox::Warning);
else if (rec.value(1).toString()=="info") msgBox->setIcon(QMessageBox::Information);
else if (rec.value(1).toString()=="error") msgBox->setIcon(QMessageBox::Critical);

msgBox->setFont(font);

msgBox->setWindowTitle(rec.value(1).toString());
msgBox->setText(rec.value(2).toString());

QSpacerItem* horizontalSpacer = new QSpacerItem(240, 290, QSizePolicy::Minimum, QSizePolicy::Minimum);
QGridLayout* layout = (QGridLayout*)msgBox->layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

msgBox->move(0,0);
msgBox->exec();

delete msgBox;
//delete horizontalSpacer;
//delete layout;


but if I uncomment last two lines my program exits. Why?

thanks in advance
best regards
Tomasz

Lykurg
26th August 2010, 22:56
your spacer and layout are children of msgBox. If you delete msgBox, all children gets deleted as well. That's a nice feature or Qt and its QObjects. Your program crashes because the spacer and layout does not exists at that time.

Tomasz
29th August 2010, 16:29
So if I got something like that (program triggered by a button):



void MainWindow::runQtApp()
{
QProcess *proc;
proc = new QProcess( this );
proc->startDetached("/home/myQtApp");
}


The memory will be 'clean' after closing that 'myQtApp'?

thanks in advance
best regards
Tomasz

Talei
29th August 2010, 16:56
No.

Starts the program program with the arguments arguments in a new process, and detaches from it. Returns true on success; otherwise returns false. If the calling process exits, the detached process will continue to live.
...
Windows: Arguments that contain spaces are wrapped in quotes. The started process will run as a regular standalone process.
It will be delated when parent is deleted, so In Your case MainWindow.

Use:

void MainWindow::runQtApp()
{
QProcess *proc;
proc = new QProcess( this );
proc->startDetached("/home/myQtApp");
delete proc;
}

Urthas
29th August 2010, 18:06
See also the documentation for QPointer.