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.

http://bytes.com/topic/c/answers/61650-vector-swap-heap-stack

http://stackoverflow.com/questions/253157/how-to-downsize-stdvector

http://www.velocityreviews.com/forums/t453533-trimming-vectors-excess-capacity.html

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*/
((*iter).second).swap(a);
}

/*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*/

return;
}

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.

http://www.codeproject.com/KB/install/EasyInstaller.aspx

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.

http://gist.github.com/290232

 

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.

http://gist.github.com/290097

If a little background is what you want to begin with, here are a few links

http://en.wikipedia.org/wiki/Windows_service
http://support.microsoft.com/kb/251192

http://msdn.microsoft.com/en-us/library/ms685141%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms810429.aspx

http://www.devx.com/cplus/Article/9857/1954 ( I preferred this. And my article adopts this code in a reasonably simple way  )

http://www.informit.com/articles/article.aspx?p=337306

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 2 other followers

del.icio.us

Twitter Updates

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

Categories


Follow

Get every new post delivered to your Inbox.