How force sanitiser to detect memory leaks when memory allocation takes place in a predefined storage? Below an example
#include <iostream>
#include <type_traits>
class A
{
};
typename std::aligned_storage<256, alignof(256)>::type store;
A * foo()
{
return new (&store) A();
}
A * bar()
{
return new A();
}
int main() {
A * a = foo();
a = foo();
A * a2 = bar();
a2 = bar();
}
When bar is called memory leak is detected. But no when foo is called Demo
Your
A
is "trivially destructible", which means (quote taken from cppref docs forstd::is_trivially_destructible
):Moreover, there is no memory leak from calling
foo
several times, becausefoo
itself does not allocate any memory.This code is totally fine and there is no reason for a sanitzier to complain:
Output is