We have a class to represent a list in a very large application:
CSMLNode
{
CSMLNode *pChild;
CSMLNode *pSibling;
CSMLNode()
{
pChild = NULL;
pSibling = NULL;
}
}
For optimization, the node objects created are kept in a pool and reused over the lifetime of the application. The node objects are released only when the number of node objects exceeds a particular count. When the node objects are released to the pool or taken from the pool, the value of pChild and pSibling are not set to NULL. We susepect that this is creating crashes in the application.
I am planning to set the value of pChild and pSibling to 0xDDDDDDDD, if its values are not NULL when taken a node object from the pool. My intensrion is to forcefully crash the application, when pChild or pSibling pointers are accessed without setting proper object pointers to pChild and pSibling after taking node object from from the pool.
Will this work as I am expecting? If it doesn’t, what pointer value should I give for forceful access violation.
I have enabled full page heap to get the access violation as early as possible.
The Visual Studio debug CRT already does this. Memory you allocate from the heap is initialized to 0xcdcdcdcd. Since your struct contains pointers, dereferencing an uninitialized pointer will automatically generate an AV. The debug heap initialization values are documented here.
You don’t have to help. Avoid keeping your own pool, the CRT already does a fine job with ample help from the Windows low-fragmentation heap. If you want to keep it then I’d suggest you use the same approach as the debug CRT. Don’t skip on everything else it does, the no-man’s land areas are an excellent way to detect memory corruption early.