Results 1 to 4 of 4

Thread: Changing memory allocation managment ( QT 4.4.3 )

  1. #1

    Default Changing memory allocation managment ( QT 4.4.3 )

    Hi everyone.

    I am using Qt 4.4.3 embedded to write a GUI on ubuntu .
    It consists in a main menu with 8 icons, and each of them leads to another menu.
    In order to use as few Ram memory as possible, I coded my application so that when I am entering a menu, I use " new" to allocate memory to this menu and use "delete" to come back to the main menu.
    To give an exemple, if I want to go from the "main menu" to the "configuration menu":

    in mainmenu.cpp:
    (ConfigationMenu is declared in mainmenu.h)
    (These slots respond to clicked() signals from mainmenu and ConfigurationMenu)

    /**********SLOTS******/

    void mainmenu::toConfigurationMenu()
    {
    win_ConfigurationMenu=new ConfigurationMenu(this);
    .....
    }

    void mainmenu::backToConfigurationMenu()
    {
    delete(ConfigurationMenu);
    ....
    }


    I thought this code would free the memory allocated by theConfigurationMenu object but when I am displaying the amount of memory my program is using while running ( using the command ps on my process id ), I can the memory is not desallocatted.
    Actually after investigation, I discovered that when I create a submenu object, memory is allocated but never desallocated. However it is not a leak because if I enter again in this submenu, memory usage does not change.

    In addition if I entered submenu1 befor and enter submenu2, memory allocated for submenu1 ( wich, to me, if not desallocated should at least be writable again by another object ) is not written by the data of my submenu2 object.

    Which means in conclusion: if I enter at least one time in all my submenus, the memory usage is the same of the one that would be used if create all my submenu objects in the constructor of mainmenu when the program is launched.

    Is this comportment usual? Would it be possible to "really" desallocate memory when I delete an object?

    Thanks for your help!
    Regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Changing memory allocation managment ( QT 4.4.3 )

    Your code sinpets show that you are still not clear on basic C++ syntax.
    So first make sure you are proficient with C++ syntax, with out it you will have lots of trouble programming C++.
    You are not deleting the menu you allocated.
    To delete the menu you allocated you have to do:
    Qt Code:
    1. if(win_ConfigurationMenu){
    2. delete win_ConfigurationMenu;
    3. win_ConfigurationMenu = NULL;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Of course, you have to make sure win_ConfigurationMenu is a class member, and don't forget to set it to NULL when ever it is not allocated.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3

    Default Re: Changing memory allocation managment ( QT 4.4.3 )

    Hi,
    ok I improved my code like you advised.
    However the memory is still not freed each time I am deleting a QObject.
    I insist on the fact there are no leaks: the memory usage is constant when I have entered at least one time all the menus.

    Do you know how I could ask neither Qt or my OS to 'really' free this memory?
    Last edited by Link130; 19th October 2009 at 16:37.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Changing memory allocation managment ( QT 4.4.3 )

    Its hard to say exactly what is going on in your code.
    If you have other such mistakes as the one I saw, then everything is possible.
    Try using 'top' with the Mem option to monitor your application memory usage.

    However, the C++ standard states that 'delete' deallocates the memory of a valid object pointed to by the pointer.
    Note also, that it could be that the OS is using some sort of caching or paging, that still holds that memory for the process, but might reroute that memory to another process dependent on various strategies.

    Bottom line:
    if your code is correct, and you manage your allocation and deallocation correctly, you should not worry about it, beyond that its the domain of the OS.

    Unless of course, you are running some sort of an RTOS, where you have to configure or actively tell the OS what and how to do when it comes to memory allocation/deallocation.
    But then its a very system specific issue.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Virtual memory allocation problem
    By morfei in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2009, 11:30
  2. QDrag : memory allocation
    By kghose in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2008, 22:57
  3. Memory allocation failure and crash in QVector
    By ashatilo in forum Qt Programming
    Replies: 16
    Last Post: 20th October 2007, 23:27
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 09:21
  5. vector memory allocation
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 23rd March 2006, 17:27

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.