Azure / vld

Visual Leak Detector for Visual C++ 2008-2015
https://kinddragon.github.io/vld/
GNU Lesser General Public License v2.1
42 stars 25 forks source link

Revisit false-positives in static initialization #4

Open mattdurak opened 4 years ago

mattdurak commented 4 years ago

Ideally VLD should detect this leak:

#include <stdlib.h>
static void* v = malloc(1); // allocation of raw pointer, no automatic free
int main()
{
    // do something with v
    return 0;
}

But should not detect this:

#include <string>
static std::string s("my string"); // allocates string but destructor will be called at exit
int main()
{
    // something using s
    return 0;
}

The current solution will not detect either of those cases as leaks.

We could do better by seeing static initializations with constructors in the stack and assuming that destructors will be called. That is slightly more complex than the existing logic, but could be done without too much difficulty. Then we could still consider the code above a leak, that could be "fixed" with an explicit free call.