https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104718
Bug ID: 104718 Summary: Leak reported for elided static variable Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: sanitizer Assignee: unassigned at gcc dot gnu.org Reporter: dlong at cadence dot com CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org, jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org Target Milestone: --- Created attachment 52523 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52523&action=edit test case showing incorrect leak report This is a case where g++ is able to detect that a section of code is not used, and as a result it seems to be more-or-less eliding a static variable that references some dynamically allocated storage. It seems that this is confusing the leak sanitizer, which reports the storage for that variable as leaked. Normally, static variables which reference dynamically allocated memory at program exit are not shown as leaks. If you compile the attached test with: g++-9.3.0 -std=c++17 -O2 -fsanitize=address -DUSE2=true lsantest.cc -o lsantest so that the mutex mut2 is not elided and then run ./lsantest, the program exits cleanly and reports no leaks. If you compile it with: g++-9.3.0 -std=c++17 -O2 -fsanitize=address -DUSE2=false lsantest.cc -o lsantest so that mut2 can be elided and then run ./lsantest, the sanitizer will report a leak at exit. mut1 is never elided and never reported as a leak. mut2 will not be reported as a leak if use2 is just static bool instead of static bool const. Nor will it be reported as a leak if mut2 is not declared static.