https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122061

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That means you cannot warn when compiling the original example, because the
destructor is not necessarily wrong.

But if you compile code that actually uses the constructor and destructor, then
GCC already warns:

struct S
{
    S();
    ~S();
    char * p1;
    char * p2;
};

S::S()
{
    p1 = new char [ 10];
    p2 = new char;
};

S::~S()
{
    delete p1;
    delete [] p2;
}


In destructor 'S::~S()',
    inlined from 'void d()' at s.cc:24:1:
s.cc:17:12: warning: 'void operator delete(void*, long unsigned int)' called on
pointer returned from a mismatched allocation function
[-Wmismatched-new-delete]
   17 |     delete p1;
      |            ^~
In constructor 'S::S()',
    inlined from 'void d()' at s.cc:23:4:
s.cc:11:23: note: returned from 'void* operator new [](long unsigned int)'
   11 |     p1 = new char [ 10];
      |                       ^
In destructor 'S::~S()',
    inlined from 'void d()' at s.cc:24:1:
s.cc:18:15: warning: 'void operator delete [](void*)' called on pointer
returned from a mismatched allocation function [-Wmismatched-new-delete]
   18 |     delete [] p2;
      |               ^~
In constructor 'S::S()',
    inlined from 'void d()' at s.cc:23:4:
s.cc:12:14: note: returned from 'void* operator new(long unsigned int)'
   12 |     p2 = new char;
      |              ^~~~


So it already seems to be working fine. Warning unconditionally when you see
the destructor would be wrong.

So I think this is INVALID.

Reply via email to