Attempt to suppress the false false leak warning

I am using clang static analysis in Xcode 6.4 (6E35b) and am getting a false positive warning about a potential memory leak. I am explicitly freeing the memory in question, but the freeing happens in a different compilation unit. Here is my MWE:

file2.c: Performs actual deallocation.

#include <stdlib.h>
void my_free(const void* p) {
    free((void*) p);
}

      

file1.c: Allocates memory and explicitly frees it through an external function.

#include <stdlib.h>
void my_free(const void* p);

int main(int argc, char* argv[]) {
    void* data = malloc(1);
    if(data) my_free(data);
    return 0; /* <-- "Potential leak of memory pointed to by 'data'" */
}

      

When I define my_free()

in the same compilation unit as calling it, no warning is generated, but of course I need to call my_free()

from a lot of different source files.

I have read the FAQ and how to deal with common false positions , but this does not apply to my situation. What can I do to assure that I am actually freeing this memory?

If the version information is relevant:

% clang --version
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)

      

+3


source to share


1 answer


One way to fix this is to add parser-specific code to the header file:

#ifdef __clang_analyzer__
#define my_free free
#endif

      



This will make the static analyzer think that you are using a classic function free

and stop complaining.

+3


source







All Articles