STL vector swap trick

Sometime back, I had noticed that even upon clearing the contents of an STL vector or list, the memory is not immediately returned to the OS’s memory pool. My processes memory foot-print keeps increasing. I had looked up a few places to find a solution to this.

Using the vector swap trick comes really handy in a situation like this:

Pasting the code snippet below:

void Foo::InitializeTable(void)
wxMutexLocker locker(evt_table_Mutex);

/*the map’s lifetime is the same as Foo’s*/

map< wxString, list< unsigned int > >::iterator iter;

/*Now, I’d like to clear all the lists in the map

*calling clear() here results in the destruction

*of the objects stored. But, does not trim your list

* to size 0. So, any new objects added to the list

*only result in the list taking up more memory

*than needed.*/

for( iter = evt_table.begin(); iter != evt_table.end(); ++iter )

(*iter).second.clear();   /*clear calls the respective destructors*/
list< unsigned int > a;   /*create a temp empty list*/
a.swap((*iter).second);  /*swap contents*/

/*temp list goes out of scope on return.And you now have a new trimmed list*/

/*the overhead incurred in swapping lists might/might not be worth*/


Arun Poruri


open-source Installer for your application on windows

You can use this open-source EasyInstaller to create an installer for your applications on windows.

For Windows 7,

dwMajorVersion = 6
dwMinorVersion = 1

If you see that the installer is not useful to install your application on windows 7,

1. Add a new checkbox resource using your visual studio resource editor   &

2. Make the changes to the following files I am enclosing in this link. And your application should get installed on Win7  without a problem.


Writing a win32 service application

I had to convert two exiting applications to win32 service applications recently. This allows us to use the MS management tools to control these services. I spent a day trying to figure out how this is done.

If you would like to dive straight into the details, please follow this link.

If a little background is what you want to begin with, here are a few links ( I preferred this. And my article adopts this code in a reasonably simple way  )

Few tips I found useful:

1. Change the process’ current working directory to your application’s installation directory. This makes it easy to avoid any confusions related to CWD. Because by default the working directory for Win32 service applications is C:\Windows\System32 or C:\Windows\SystemWow64.

SetCurrentDirectory(“C:\\Program Files\\MyDir”);

2. To add any dependencies while creating services, use the ‘depend= ‘ option.

Ex: sc create myservice binPath= “C:\Program Files\MyApp\MyApp.exe” depend= idependonthisapp

3.  Prefer to separate all the service related functionality into a new thread while your main thread remains as it is.


Join 3 other followers

Twitter Updates

Error: Twitter did not respond. Please wait a few minutes and refresh this page.