views:

46

answers:

1

Hello,

is there a way to detect simple memory leaks like this one with a static analysis tool? I cannot change the code to include the tipical includes used in runtime memory leak detection (struc1 is a simple structure with some fields).

void noRelease(void)
{
    struc1 *memoryLeak;

    memoryLeak = (struc1 *) malloc(sizeof struc1);
    if (NULL != memoryLeak)
    {
        memoryLeak->a=3;
    }
}

VSTS (Visual Studio Team System) detects memory leaks due to exceptions but is not able to see this simple leak.

Any ideas will be very helpful. Thanks a lot.

+1  A: 

Hm... Coverity could do that but you would have to sell your house to pay for it. I once wrote a static analyzer that checks if a pair of functions are called in a given function scope. I used a static analysis API that comes with a program called 'Understand 4 C++' made by scitools. www.scitools.com I wrote the searcher/scrutinzer using a managed API (that I wrote) that wraps their C API. Note: However Understand 4 c++ is not free.

Anyways, that tool I wrote would detect the lack of free in the code above. it was not much smarter than that. If the pointer was free'd somewhere else, it would not find it.

C Johnson
Ok, so Coverity looks like the only solution ... thanks.
SoMoS