PDA

View Full Version : Memory error for Qlabel



Qt Coder
3rd April 2009, 14:56
Hello All,


I m using Qt 4.4.0

I have below code


//in Header file
class C1
{
private:
QLabel *img_label_1;
QLabel *img_label_2;

QLabel *img_label_3;
QLabel *img_label_4;
}


//in CPP file


C1::C1(QWidget * parent):QDialog(parent)
{
ui.setupUi(this);
setWindowFlags( Qt::FramelessWindowHint);

img_label_4 = NULL;
img_label_1 = NULL;
img_label_2 = NULL;
img_label_3 = NULL;


if((img_label_1 = new QLabel(ui.frame)) != NULL)
QMessageBox::information(this,"TRUE","");
img_label_1->setPixmap(QPixmap(":/images/pink-1.png"));
img_label_1->setGeometry(6,4,16,16);
img_label_1->setScaledContents(TRUE);
img_label_1->show();

img_label_1->setStyleSheet( "border-style:solid;\n"
"border-color:rgb(86,86,86);\n"
"border-width:1px;\n"
);
//In same way I m alloctiing 2 more labels
//And now the turn of 4th label

if((img_label_4 = new QLabel(this)) != NULL)
QMessageBox::information(this,"TRUE","");


}
Now the problem is
when I include
if((img_label_4 = new QLabel(this)) != NULL) statement
after closing the EXE it gives Memory error

If I comment the above line it doesnt giv any error..

The drive where program resides has 1.2 G free memory.

As soon as it allocates memory for 4th label ,it gives memory error after closing EXE.

Please help

spirit
3rd April 2009, 15:02
why you don't create these labels in ui and then in ctor set all needed settings?

Qt Coder
3rd April 2009, 15:09
same error is occuring when tried to add label from the designer.

spirit
3rd April 2009, 15:11
could you attach you project?

Qt Coder
3rd April 2009, 15:25
it's size is big & can't attach it here.

talk2amulya
3rd April 2009, 15:33
dont attacht the whole project..just attach C1.cpp, C1.h and related ui file

Qt Coder
4th April 2009, 08:45
I have submiited all concerned code...


One thing is for sure that it gives error as soon as it allocates memory for anything after the 3rd label.


I hav tried creating
QGraphicsScene *S = new QGraphicsScene(this)

and it gave error after closing..

If I debug the application for error it shows some disassembly code which is beyond understanding...


It allocates the memory for 4th label but while freeing that particular memory it is facing some problem .

Any help would be appreciated...

faldzip
4th April 2009, 09:11
And what is that this you are passing to the constructor?

P.S. And use CODE tags for code, and QUOTE tags for quotes.

talk2amulya
4th April 2009, 09:53
i still believe there is smth you havent shared with us that is causing the issue, cuz the above code looks pretty benign..could u attach the whole .h, .cpp and .ui files?

Qt Coder
4th April 2009, 11:05
Pls find attached files.

talk2amulya
4th April 2009, 11:31
when u r deleting the objects in delete_objects(), set them back to NULL too..read about dangling pointers http://en.wikipedia.org/wiki/Wild_pointer

Qt Coder
4th April 2009, 12:01
Thanx ,The problem is solved

I done following changes in delete_objects() function

void glayout::delete_objects()
{
if(img_label_1 != NULL){

delete img_label_1;
img_label_1 =NULL;
}
if(img_label_2 != NULL){
delete img_label_2;
img_label_2 =NULL;
}
if(img_label_3 != NULL){
delete img_label_3;
img_label_3 =NULL;
}

if(img_label_4 != NULL){
delete img_label_4;
img_label_4 =NULL;
}

if(img_label_5 != NULL){
delete img_label_5;
img_label_5 =NULL;
}

if(img_label_6 != NULL){
delete img_label_6;
//img_label_6 =NULL;
}
}

One Query I Hav now is
I m using multiple such forms in my application but I m not deleting objects in such a way anywhere ,but it never gave me any error before .

As soon as I added above form it started giving me error on exiting applictaion.

Also if I don't call function "delete_objects()" it gives me "Memory can not be referenced ....." error after closing EXE.

Why is it so...

talk2amulya
4th April 2009, 12:17
ok, i dont know about how u have done evth in your application..but here is how u should do memory management in Qt.
whenever u create anything on heap, in C++, you have to take care of SAFELY deleting it. but here in Qt, if u provide any object with a parent, the parent will take care of it..u dont have to worry about it at all..in your case here, u delete the object but dont set the pointer to NULL. Now, since u provided ur label with a parent, the parent tries to delete it and blows up the application..now when u set the object to NULL after deleting it urself..while parent tries to delete it, it checks if its NULL, and doesnt double delete it..thus the app doesnt blow up..

so at the heart of it, if u provide an object with a parent, Qt will take care of deleting it BUT only when the parent is deleted..if u want to delete objects urself, make sure u do that SAFELY, i.e. by deleting the memory and setting the object to NULL.

Qt Coder
4th April 2009, 12:57
Actually for first time it ran properly without any error, (after I made pointer setting to Null chnages)

and now again I hav executed it and now its crashing just after its executing the statement


if((img_label_6 = new QLabel(ui.frame)) != NULL)


i.e. when its allocating memory for 6th Qlabel its crashing ...

now what problem it has??????

talk2amulya
4th April 2009, 13:03
if(img_label_6 != NULL){
delete img_label_6;
//img_label_6 =NULL;
}
}

u have commented the NULL assignment here