Results 1 to 15 of 15

Thread: Memory error for Qlabel

  1. #1
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Memory error for Qlabel

    Hello All,


    I m using Qt 4.4.0

    I have below code

    Qt Code:
    1. //in Header file
    2. class C1
    3. {
    4. private:
    5. QLabel *img_label_1;
    6. QLabel *img_label_2;
    7.  
    8. QLabel *img_label_3;
    9. QLabel *img_label_4;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //in CPP file
    2.  
    3.  
    4. C1::C1(QWidget * parent):QDialog(parent)
    5. {
    6. ui.setupUi(this);
    7. setWindowFlags( Qt::FramelessWindowHint);
    8.  
    9. img_label_4 = NULL;
    10. img_label_1 = NULL;
    11. img_label_2 = NULL;
    12. img_label_3 = NULL;
    13.  
    14.  
    15. if((img_label_1 = new QLabel(ui.frame)) != NULL)
    16. QMessageBox::information(this,"TRUE","");
    17. img_label_1->setPixmap(QPixmap(":/images/pink-1.png"));
    18. img_label_1->setGeometry(6,4,16,16);
    19. img_label_1->setScaledContents(TRUE);
    20. img_label_1->show();
    21.  
    22. img_label_1->setStyleSheet( "border-style:solid;\n"
    23. "border-color:rgb(86,86,86);\n"
    24. "border-width:1px;\n"
    25. );
    26. //In same way I m alloctiing 2 more labels
    27. //And now the turn of 4th label
    28.  
    29. if((img_label_4 = new QLabel(this)) != NULL)
    30. QMessageBox::information(this,"TRUE","");
    31.  
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 
    Now the problem is
    when I include
    Qt Code:
    1. if((img_label_4 = new QLabel(this)) != NULL)
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by wysota; 4th April 2009 at 10:47. Reason: Changed [quote] to [code]

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory error for Qlabel

    why you don't create these labels in ui and then in ctor set all needed settings?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory error for Qlabel

    same error is occuring when tried to add label from the designer.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Memory error for Qlabel

    could you attach you project?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory error for Qlabel

    it's size is big & can't attach it here.

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory error for Qlabel

    dont attacht the whole project..just attach C1.cpp, C1.h and related ui file

  7. #7
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory error for Qlabel

    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
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by wysota; 4th April 2009 at 10:48. Reason: Changed [quote] to [code]

  8. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory error for Qlabel

    And what is that this you are passing to the constructor?

    P.S. And use CODE tags for code, and QUOTE tags for quotes.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. #9
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory error for Qlabel

    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?

  10. #10
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory error for Qlabel

    Pls find attached files.
    Attached Files Attached Files

  11. #11
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory error for Qlabel

    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

  12. The following user says thank you to talk2amulya for this useful post:

    Qt Coder (4th April 2009)

  13. #12
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Memory error for Qlabel

    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...

  14. #13
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory error for Qlabel

    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.

  15. #14
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory error for Qlabel

    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

    Qt Code:
    1. if((img_label_6 = new QLabel(ui.frame)) != NULL)
    To copy to clipboard, switch view to plain text mode 


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

    now what problem it has??????

  16. #15
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory error for Qlabel

    Quote Originally Posted by Qt Coder View Post
    if(img_label_6 != NULL){
    delete img_label_6;
    //img_label_6 =NULL;
    }
    }
    u have commented the NULL assignment here

Similar Threads

  1. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  2. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  3. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.