Monthly Archives: December 2008

Use Application Verifier to detect a heap bug

What happens if you run a code like this? int main(){ unsigned char* p = new unsigned char[16]; // fill 17 bytes where only 16 bytes are allocated! memset(p, 0xff, 17); delete [] p; } Even if you compile the … Continue reading

Posted in Advanced Debugging | Leave a comment

Use .ocommand for automatic debugging

A debugger is not just for debugging an application. It’s also useful for monitoring the application execution. For example, you may want to get the stack trace whenever certain function returns error. HANDLE hFile = ::CreateFile("foo.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, … Continue reading

Posted in Advanced Debugging | Leave a comment

Performance: Virtual Allocation vs Heap Allocation

Virtual Allocation is slower than Heap allocation because heap manager is well optimized for small memory block allocations. So, how fast is it? Here is the benchmark. The first test (List 1) is allocating 0KB to 512KB memory block 5,000,000 … Continue reading

Posted in Windows Memory Management | 2 Comments

How to shutdown a worker thread when a form is closed?

Use BeginInvoke instead of Invoke. Let’s assume you are writing a .NET application with a progress bar. It starts a worker thread when the application starts. The worker thread does some lengthy work. The worker thread calls OnProgress() delegate whenever … Continue reading

Posted in Tips | Leave a comment

The Windows Heap never shrinks

The Windows Heap manager is used to allocate/de-allocate relatively small size of memory blocks. It has an interesting (and sometimes annoying) characteristics – the heap never shrinks. Look at the code below. It allocates a lot of 480KB memory blocks … Continue reading

Posted in Windows Memory Management | Leave a comment