Here is an example of a false positive "possibly lost":
class A { int x; }; class B { int y; }; class C : public A, B { int z; }; int main() { static B* notLost = new C();
Here is a more general example of a false positive:
//get asprintf #define _GNU_SOURCE #include <stdio.h> #include <assert.h> char* getFoo() { static char* foo = NULL; if(!foo && asprintf(&foo, "Hello World\n") < 0) assert(0); return foo; } int main() { printf("%s", getFoo()); }
This is a typical singleton ideologist: there is somewhere a function that provides access to a special object (here the string "Hello World"), ensuring that there is only one such object ever created. Since an object is never destroyed / freed, Valgrind must think that it is a memory leak. They are usually listed as “still reachable” because there is still a static variable through which you can access, but still it is false.
cmaster
source share