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

Why hasn't AddressSanitizer detected a memory leak in my program? #1485

Closed
ltimaginea opened this issue Jan 5, 2022 · 1 comment
Closed

Comments

@ltimaginea
Copy link

My program is as follows:

#include <iostream>
#include <cassert>

class Singleton
{
public:
	static Singleton& Instance()
	{
		static Singleton& instance = *(new Singleton());
		return instance;	// potential leak
	}
public:
	Singleton(const Singleton&) = delete;
	Singleton(Singleton&&) = delete;
	Singleton& operator=(const Singleton&) = delete;
	Singleton& operator=(Singleton&&) = delete;
	~Singleton() = default;
private:
	Singleton() = default;
};

int main()
{
	auto& a = Singleton::Instance();
	auto& b = Singleton::Instance();
	assert(&a == &b);

	//delete (&Singleton::Instance());

	return 0;
}

Test environment: WSL2 Ubuntu 20.04.3 x86_64-linux-gnu-g++ 9.3.0

The method I use AddressSanitizer is as follows:

g++ -fsanitize=leak -g ./main.cpp
./a.out

or

g++ -fsanitize=address -g ./main.cpp
./a.out

The only delete expression delete (&Singleton::Instance()); has been commented out in my program. So my program should have a memory leak.

But, when I run the executable a.out , it outputs nothing.

Why hasn't AddressSanitizer detected a memory leak in my program?

@ramosian-glider
Copy link
Member

The allocated memory is still reachable from a global variable (static Singleton& instance is a global), hence it is not considered a memory leak.
You can disable this behavior by passing LSAN_OPTIONS=use_globals=false:

$ g++ t.cc -o t -fsanitize=address
$ ./t
$ LSAN_OPTIONS=use_globals=false ./t

=================================================================
==3419665==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 73728 byte(s) in 1 object(s) allocated from:
    #0 0x7f5a9b0f4cd7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x7f5a9acaf7ad  (/lib/x86_64-linux-gnu/libstdc++.so.6+0xaf7ad) (BuildId: 69568e25e14544c953a9f79da5e158d0f644ad17)

Direct leak of 1 byte(s) in 1 object(s) allocated from:
    #0 0x7f5a9b0f57f8 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x5595797f32ad in Singleton::Instance() (/usr/local/google/src/TMP/linux/t+0x12ad) (BuildId: 2e9033784f7ac303257f0b2ef41da0de10472db6)
    #2 0x5595797f31e5 in main (/usr/local/google/src/TMP/linux/t+0x11e5) (BuildId: 2e9033784f7ac303257f0b2ef41da0de10472db6)
    #3 0x7f5a9aa40c89 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: 73729 byte(s) leaked in 2 allocation(s).

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