Results 1 to 11 of 11

Thread: Memory Management Reg

  1. #1
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Memory Management Reg

    hi im using QT4.5 , Debian

    Im having some doubts in QT memory management.

    for ex:
    Qt Code:
    1. class childClass
    2. {
    3. public :
    4. QString strval;
    5. int ival;
    6. childClass();
    7. QLabel *lblstr;
    8. };
    9.  
    10.  
    11. childClass::childClass()
    12. {
    13. ival=5;
    14. strval="test data";
    15. lblstr=new QLabel(this);
    16. lblstr->setText("label name");
    17. }
    To copy to clipboard, switch view to plain text mode 

    im creating a object from another class named, mainClass

    Qt Code:
    1. mainClass::createChild()
    2. {
    3. childClass *c=new childClass;
    4. delete c;
    5. qDebug<<c->ival;
    6. qDebug<<c->strval;
    7. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory Management Reg

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

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Management Reg

    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

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory Management Reg

    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.

  5. #5
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Management Reg

    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

  6. #6
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Management Reg

    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

  7. #7
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Management Reg

    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
    Last edited by BalaQT; 4th February 2010 at 05:27.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory Management Reg

    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.

  9. The following user says thank you to squidge for this useful post:

    BalaQT (4th February 2010)

  10. #9
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Management Reg

    Thnks for help fatjuicymole.

  11. #10
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Memory Management Reg

    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

  12. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory Management Reg

    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.

  13. The following user says thank you to squidge for this useful post:

    BalaQT (6th February 2010)

Similar Threads

  1. Recommend memory management literature
    By frenk_castle in forum General Programming
    Replies: 3
    Last Post: 18th October 2009, 20:30
  2. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  3. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  4. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21

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.