PDA

View Full Version : Changing memory allocation managment ( QT 4.4.3 )



Link130
18th October 2009, 23:38
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.

high_flyer
19th October 2009, 10:55
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:


if(win_ConfigurationMenu){
delete win_ConfigurationMenu;
win_ConfigurationMenu = NULL;
}


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.

Link130
19th October 2009, 15:38
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?

high_flyer
20th October 2009, 09:44
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.