Thursday, December 20, 2007

Memory Debugging with VC8

Want to check how many hundreds of memory leaks you have?

Using the microsofts CRT debug heap is an easy way of doing it.

To get a listing of all memory leaks, you simply need to include crtdbg.h, set the debug flag, using the function _CrtSetDbgFlag and run your code in debug mode. The leaks will appear in the output tab.

To get started with it here is the minimal code to implement it:

#include 
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking
_CrtSetDbgFlag(flag); // Set flag to the new value

To turn it off:
// Turn off CRT block checking bit
tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
This will only tell you how many leaks you have. To be really useful you want to know which calls to new are causing the memory leaks. In order for MSVC IDE to point to the lines of code which allocate the memory that causes the leak, simply redefine new as such:

#define new new(_NORMAL_BLOCK,__FILE__, __LINE_)

Finally if you get a memory leak you can set a breakpoint at a secific

Finally if you get a memory leak you can set a breakpoint at a specific allocation number by using:

_CrtSetBreakAlloc(2825);
with 2825 being the allocation nb shown in {} in the error message.

Et voila!
Simple no?

Tuesday, December 11, 2007

POCO and open-source


Recently I stumbled on POCO: A C++ Portable Components, or a collection of open source C++ class libraries. One word: wow. This library seems very well thought and having tried several libraries this one is the most promising.

Actually I am using mostly boost but I begin to be worried by the inconsistence between each sub libraries considering the api, the documentation and some over bloated code.
My principal need was a cross-platform threading libraries but boost::thread is lacking in some areas (no read-write mutex, difficulty to set thread specific code, ...). All those are present in POCO.

I will definitively switch to it if my tests are satisfactory and forget about boost. In fact I am even hoping that boost will not be used at all in the C++0x revision...


Marthen Mickos the MySQL CEO posted very interesting thoughts about how to succeed with an open-source project.
They needed 12 years... I will try to explain this to my wife:
"Darling! Let me work on my pet project... I will be rich in 12 years."
Somehow I have the feeling it will be difficult to convince her :)
MySQL needed 6 years to have a stable code so OGE has still 5 years to become stable.
Good - I am not as off track as I feared :D