1

I'm trying to get clang++ to tell me there is a memory leak. I tried scan-build but it reported nothing. How do I get llvm/clang to warn me of this problem?

#include <iostream>

int main() {
        int *a = new int;
        *a = 8;
        std::cout<< a << std::endl;
}
user2814152
  • 197
  • 9

2 Answers2

0

False-positive pruning usually leads to removing all leaks that originate from main(), since the program will exit anyway. Try analyzing the same code, but in a different function.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
  • Leaking pointer allocated with new in a non `main` function is not reported for me either. There is [unix.Malloc check](http://clang-analyzer.llvm.org/available_checks.html) I can't find anything related to `new` check. Static analysis just doesn't cover everything. The reason why dynamic analysis with dtrace or other tools exists. – dmitri Oct 07 '13 at 22:57
0

Because int is too small, there is something like one "reserved section" for program so for small object no need to extend memory area, try to apply 1024 * 1024 * 10 then check the result

http8086
  • 1,306
  • 16
  • 37