PDA

View Full Version : Memory Management Reg



BalaQT
3rd February 2010, 06:50
hi im using QT4.5 , Debian

Im having some doubts in QT memory management.

for ex:


class childClass
{
public :
QString strval;
int ival;
childClass();
QLabel *lblstr;
};


childClass::childClass()
{
ival=5;
strval="test data";
lblstr=new QLabel(this);
lblstr->setText("label name");
}


im creating a object from another class named, mainClass



mainClass::createChild()
{
childClass *c=new childClass;
delete c;
qDebug<<c->ival;
qDebug<<c->strval;
}


Even after the delete command, qDebug able to print the ival,strval of childClass.
How to clear the variables ival,strval to free the memory?


Thnks
Bala

squidge
3rd February 2010, 08:09
This is standard C++, nothing to do with Qt. You have freed the memory, you shouldn't be trying to deference the pointer any more as you no longer own it, so it could cause SIGSEGV (Segmentation fault).

BalaQT
3rd February 2010, 08:16
thnks for the reply fatjuicymole ,
But im not getting the SEG FAULT. instead i can able to get the value of ival,strval even after the DELETE.

why the memory occupied by ival,strval is not cleared ,even after DELETE.
(or) how can i free ival,strval to optimize memory?
Thnks
Bala

squidge
3rd February 2010, 12:01
It will be cleared eventually, and certainly when your program exits. For such small memory usage as you have above, it will remain in the C runtime library until needed again. You may notice that the values of the variables freed change as your program runs due to the memory being used for other things. Therefore you should never access those variables after you have freed the pointer.

BalaQT
3rd February 2010, 13:35
thnks for the reply fatjuicymole

im aware of not to access the variables which are freed.
my doubt is , when Im using more int and qstrings in my embedded project, how these variables will be managed by qt?
actually they are freeed? (or) is there any responsibiltiy for me to clear the QString and int variables?

Thnks
Bala

BalaQT
3rd February 2010, 13:35
thnks for the reply fatjuicymole

im aware of not to access the variables which are freed.
my doubt is , when Im using more int and qstrings in my embedded project, how these variables will be managed by qt?
actually they are freeed? (or) is there any responsibiltiy for me to clear the QString and int variables?

Thnks
Bala

BalaQT
4th February 2010, 05:22
should i take care of freeing the space for QSTRING , int , QLIST variables?,
or
delete obj will free them?

Even in my embedded project(which hav many int and qstring)
i can able to access the variables even after the DELETE command.
why its happening? i hav plenty of qstring.

i need to utilize more memory in my project , because machine has only 64 mb ram.


It will be cleared eventually, and certainly when your program exits.
so there is no need to worry ?

Thnks
Bala

squidge
4th February 2010, 08:04
There is no need to worry. All you need to remember is not to access variables after there container has been deleted. Once you execute the 'delete' the memory is marked as reusable and can be overwritten at any time.

For performance reasons, memory is not cleared when you delete it, but simply overwritten when it is required by other objects.

BalaQT
4th February 2010, 08:20
Thnks for help fatjuicymole.

BalaQT
4th February 2010, 12:25
fatjuicymole , can u explain, if im simply calling delete OBJ , is it a memory leak for the case of INT , QSTRING ?
or should i use int *i=new int, QString *st=new QString like that . and then use delete i,delete st , delete obj for freeing all?

i want my prg ,without any memory leak. kindly ans me

Thnks
Bala

squidge
4th February 2010, 12:43
If you have "int" "QString" etc in an object and delete the object, those items will be deleted for you. However if you have "QString *", "int *" etc in your object, then you need to delete those yourself before deleting the object. Best place for that is typically the destructor of the object.