Hi there,
in the past we encountered various memory-leaks in our software. We found out that these happened mostly due to incorrect usage of our own "free"-Methods, which free Queue-Message-Data and the likes.
The problem is, in our deepest tool-functions there are two methods to free up dynamically allocated memory, with the following signatures:
void free (void *pData);
void free (void **ppData);
Both methods basically do the same thing, except the second one dereferences the data first. I know that it is possible to do everything with just one of these two, but lets just say that the software was designed that way many years ago and now theres code everywhere using both.
The problem comes in, when somebody implements a call to these methods like this:
QueueMessage *pMsg;
pMsg = myQueue.read(...); // dynamically allocates space for the msg and fills it
// Do something
myQueue.free(&pMsg); // << WRONG!
The code above should pass the pointer to the message to the free-method. Basically it would work, but since the compiler doesn't know which function to use in this case, it uses the free(void *pData)
method which then tries to free the Pointer, not the memory which the Pointer points to. Of course, the solution is easy, either:
or
myQueue.free(pMsg);
myQueue.free((void**)&pMsg);
Both will work. Now that I've described the problem and the solution, I'd like to know: Is there any way I can ensure that the programmer using these methods uses them the right way? I've read about header annotations in VS2005, which are quite useful, but don't seem to do what I need. It would be great if there is a way to produce a warning if the reference of a pointer is passed to the first method, so the programmer at least gets a hint that there's something wrong with his code.
By the way, I'm using Microsoft VS2005 and have the possibility to upgrade to VS2008 if needed. It's a C++ Application migrated to VS2005, and therefore .NET-compatible.