I've constructed a little dynamic object manager but I'm not sure if it's correctly allocating and deallocating. I've checked the code over and over and I know what I'm doing and I have no reason to think it isn't working right.
However, I made a little test program that loads a massive number of the same object and then unloads it. I then opened up Task Manager and got these results:
Test 1
Start: 540k
After loading: 10292k
After unloading: 1612k
Which is undesirable but I don't know if that's my object manager causing it or the way Windows allocates memory to my program. So I made another test which loads and unloads multiple times to see if any leaks build up:
Test 2
Start: 544k
After loading: 10292k
After unloading: 1612k
After reloading: 10308k
After unloading again: 1976k
After reloading again: 10356k
After unloading one more time: 1636k
Notice that although each time the memory seems to build up, the last time we unload the memory goes down from the last time it had unloaded. I'm getting the impression that there is far more complicated stuff going on here that's giving me the weird results, so can anyone tell me how exactly one goes about checking for memory leaks?
You should run the code at least a few hundred times in a row and see if the memory usage increases.
It's entirely possible that it's just a peculiarity of memory management.
If your code is portable, you can run it through valgrind under Linux, which can find memory leaks reliably.
Task Manager only uses a tiny subset of the metrics available on Windows NT. At any rate, it doesn't show what you need in this case, you need to use Performance Monitor.
What do you mean, you run perfmon, you select your process from Processes, select private bytes. It should turn up on the graph. Let it run as you do your tests. If it goes up and doesn't come down you have a memory leak.
Why you people arguing on something which is not useful in finding memory leaks? Even if you find that there may be memory leak, you will still have to use source memory leak detector. Then why not start with that only?
I personally use valgrind as part of my unit-testing:
to unit-test - make check
to test for memory leaks - make memcheck
to test for race conditions/threading problems - make drdcheck
I always run make check and memcheck, and I will run drdcheck periodically to ensure that new multithreading code works and that new code is not breaking old multithreading code
Even better is to use something like hudson continuous integration to automate testing on commit and daily tests