google / sanitizers

AddressSanitizer, ThreadSanitizer, MemorySanitizer
Other
11.52k stars 1.04k forks source link

How to deal with this problem? #1715

Open November20 opened 10 months ago

November20 commented 10 months ago

xiti18_10.cpp:

#include <vector>
using std::vector;
class iStack{
public:
    iStack(int capacity):top(0),stack(capacity){}
private:
    int top;
    vector<int> stack;
};
int main()
{
    iStack *ps = new iStack(20);
    return 0;
}

Compile command:

g++ -std=c++98 -O0 --ansi -fno-elide-constructors -Wall -fsanitize=address -fsanitize=undefined -g xiti18_10.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out

Results:

------------run---------------- xiti18_10.cpp
xiti18_10.cpp: In function ‘int main()’:
xiti18_10.cpp:14:17: warning: unused variable ‘ps’ [-Wunused-variable]
   14 |         iStack *ps = new iStack(20);
      |                 ^~
------------over--------------- return: 0

There is no delete here.How to deal with this problem?

November20 commented 10 months ago

This is a memory leak. Why can't it be analyzed?