2

why -fsanitize=address,leak behaves differently for T* and T*&?

I created a struct for pointer_ownership

template<class T>
struct pointer_ownership
{
    T* ptr;
    bool is_owner;
    pointer_ownership(T*& t_ptr, bool t_is_owner)
        : ptr(t_ptr), is_owner(t_is_owner)
    {
    }
};

And a main function

int main()
{
   auto *a = new int(32);
   pointer_ownership po(a, true);
   return 0;
}

When I build the code using below command and run it no leak sanitizer error is generated.

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

But if I change T* to T*& in pointer_ownership struct then leak sanitizer error is generated.

Why leak sanitizer isn't showing error in case of T*?

kush_1244
  • 39
  • 3
  • This is a QOI issue perhaps? GCC 13 does show report a leak here https://godbolt.org/z/GYsxh94ME What version of GCC are you running? – cigien May 20 '23 at 22:08
  • g++ (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0 – kush_1244 May 20 '23 at 22:11
  • 2
    clang is showing in the both cases. I suggest to use it. – 273K May 20 '23 at 22:43
  • It shouldn't make any difference (consistent with previous comments noting that your symptom goes away with a different compiler and also with an updated version of your compiler). You may wish to add a destructor to your `pointer_ownership` class that releases the pointer (that will reduce the chances of the dynamically allocated `int` being detected as unreleased by the sanitizer). – Peter May 20 '23 at 23:08
  • In the -fsanitize option if address is removed then leak sanitizer is working fine even in the case of "T*" but if address is added then leak sanitizer doesn't work and program returns 0 – kush_1244 May 21 '23 at 00:08

0 Answers0