Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No leak report on this usage: static int &a = *(new int); #1806

Closed
lijh8 opened this issue Oct 17, 2024 · 1 comment
Closed

No leak report on this usage: static int &a = *(new int); #1806

lijh8 opened this issue Oct 17, 2024 · 1 comment

Comments

@lijh8
Copy link

lijh8 commented Oct 17, 2024

int main() {
  // static 
  int &a = *(new int);
  // delete &a;
}

Hi, if i add the static keyword for the declaration

  static int &a = *(new int);

it will not report memory leak.

Is this usage c++ standard conformant behavior?

Thanks

$ gcc --version
gcc (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3)
$

$ make
g++ -fsanitize=address -std=c++23 -g  -MMD -MP   -c -o main.o main.cpp
g++  -fsanitize=address   main.o   -o main
$

$ src/main/main

==2864==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1 byte(s) in 1 object(s) allocated from:
    #0 0x7f25158f84b8 in operator new(unsigned long) (/lib64/libasan.so.8+0xf84b8) (BuildId: a4ad7eb954b390cf00f07fa10952988a41d9fc7a)
    #1 0x40117f in Foo::fun() /home/ljh/Documents/hello_cpp/src/main/main.cpp:6
    #2 0x401152 in main /home/ljh/Documents/hello_cpp/src/main/main.cpp:12
    #3 0x7f2515239087 in __libc_start_call_main (/lib64/libc.so.6+0x2a087) (BuildId: 77c77fee058b19c6f001cf2cb0371ce3b8341211)
    #4 0x7f251523914a in __libc_start_main_alias_1 (/lib64/libc.so.6+0x2a14a) (BuildId: 77c77fee058b19c6f001cf2cb0371ce3b8341211)
    #5 0x401084 in _start (/home/ljh/Documents/hello_cpp/src/main/main+0x401084) (BuildId: cddb8fb48744ed0ccb7673c7537b966fe25c3d5c)

SUMMARY: AddressSanitizer: 1 byte(s) leaked in 1 allocation(s).
$ 
@lijh8 lijh8 changed the title No leak report on this usage static Foo &fun = *(new Foo()); No leak report on this usage: static Foo &fun = *(new Foo()); Oct 17, 2024
@lijh8 lijh8 changed the title No leak report on this usage: static Foo &fun = *(new Foo()); No leak report on this usage: static int &a = *(new int); Oct 18, 2024
@ramosian-glider
Copy link
Member

By default, LSan treats allocations reachable from global memory as live and does not report them: https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizerDesignDocument

To disable this behavior, run with LSAN_OPTIONS=use_globals=0. Note that in this case LSan may report additional leaks from libc.

Note that because of #1838 this option is not shown if you run with ASAN_OPTIONS=help=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants