unit testing - How to check for memory leaks in a class in C++? -


i aware of several other threads have discussed similar questions. aware 1 must deallocate allocated resources , nothing should left unaccounted for. reason asking question see if there way automatically detect memory leak in class. in particular, trying implement automated way detect leak within unittest future development of class more robust. since class using several other classes within , dll's well, difficulat, if not impossible keep track of leaks in of those, such unittest may help.

i thinking of couple of solutions able check memory leaks within class. assuming class similar this:

class myclass {     myclass() { /* lots of allocations */ }     ~myclass() { /* lots of deallocations */ } } 
  1. in test function, instantiate class instance many times , destruct everytime. meanwhile, check task manager (in windows @ least) see if allocated memory application ramps or remains substantially flat:

    test( myclass, myclass) {     int some_big_number = 10000;     (int i=0; i<some_big_number; i++)     {         myclass *myc = new myclass;         delete myc;     } } 
  2. allocate dummy variable before , after instantiation , destruction of class instance , check if 2 addresses same. this:

    test( myclass, myclass) {     int address_1 = 0, address_2 = 0;      int *tmp_1 = new int;     address_1 = tmp_1;     delete tmp_1;      myclass *myc = new myclass;     delete myc;      int *tmp_2 = new int;     address_2 = tmp_2;     delete tmp_2;      expect_true( address_1 == address_2 ); } 

    my assumption if there no memory leak within myclass , claimed memory has been deallocated, addresses of tmp_1 , tmp_2 should same. not sure if memory allocation works way.

  3. use plugin or program such deleaker or valgrind, may make code or test unfavorably large , not sure if can used in context above anyway.

thanks!

your second idea won't work because won't same address each time.

however first idea start, , try improve upon here. instead of watching task manager, can automate part of calling getprocessmemoryinfo(). see this answer information on how that. value want @ in returned data structure privateusage. if before instantiating class object , again after deleting it, memory usage should same. also, if other methods on class memory allocation, you'll want call them in there too. way test case can give class workout without human intervention, , can make whatever want (log something, raise alert, whatever) if detects memory usage rising.

something this:

// starting memory usage int baseline = getmemoryusage();   // function calls getprocessmemoryinfo() described in link above  myclass *myc = new myclass; int memusage = getmemoryusage(); myc->dostuff(); if (getmemoryusage() != memusage)     alert("detected memory leak in myclass::dostuff()"); memusage = getmemoryusage(); myc->domorestuff(); if (getmemoryusage() != memusage)     alert("detected memory leak in myclass::domorestuff()"); // ... // etc - call each method kind of memory allocation // ... delete myc; if (getmemoryusage() != baseline)     alert("detected memory leak in myclass"); 


implement getmemoryusage() function referenced above, can (i don't have compiler in front of me right now, forgive typos):

#include <windows.h> #include <psapi.h> #pragma comment(lib, "psapi.lib")   int getmemoryusage(); {     process_memory_counters_ex pmc;      dword processid = getcurrentprocessid();     handle hprocess = openprocess(process_query_information |                                     process_vm_read,                                     false, processid);     if (hprocess == null)         return -1;      if (getprocessmemoryinfo( hprocess, (process_memory_counters *) &pmc, sizeof(pmc)))     {         closehandle(hprocess);         return pmc.privateusage;     }      closehandle(hprocess);     return -1; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -