Bug 58114 - allow turning the warning about deleting a pointer of incomplete type into an error
Summary: allow turning the warning about deleting a pointer of incomplete type into an...
Status: RESOLVED DUPLICATE of bug 43452
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.2
: P3 normal
Target Milestone: 4.9.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2013-08-09 17:00 UTC by Tilman Vogel
Modified: 2016-12-23 02:11 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-04-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tilman Vogel 2013-08-09 17:00:58 UTC
While many warning messages have a way to control their emission and treatment, e.g. "warning: multi-line comment [-Wcomment]", some warnings do not, in particular:

warning: possible problem detected in invocation of delete operator: [enabled by default]
warning: 'value' has incomplete type [enabled by default]

As a consequence of being enabled by default, there does not seem to be an associated warning class that I could pass to "-Werror=". 

I'd really like to be able to say 
"-Werror=delete-with-incomplete-type"
Comment 1 Teodor Petrov 2014-04-16 15:28:51 UTC
This option is very important, because this warning allowed us to fix one serious leak in our application. And to prevent this problem to reappear in the future we want to force this warning to be error, but unfortunately we are not able when building with GCC.

All other compilers we use have this feature. And btw in clang the option is named -Wdelete-incomplete, so you can reuse it to minimize the difference between the compilers.

BTW: Do someone has an explanation why this is allowed in the standard?
Comment 2 Jonathan Wakely 2014-04-16 16:39:19 UTC
(In reply to Teodor Petrov from comment #1)
> BTW: Do someone has an explanation why this is allowed in the standard?

Because it's only a problem if the type has a non-trivial destructor or an overloaded operator delete. For a type with a trivial destructor (such as a POD) all that needs to be done is deallocate the memory.

The standard says:

"If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined."

Since the compiler can't know if an incomplete type has a trivial destructor or not, it can only warn, not give a hard error. If you want to make it a hard error in your own program you should be able to, so confirming.
Comment 3 Jonathan Wakely 2014-04-16 16:42:08 UTC
Actually it looks as though -Wdelete-incomplete is already supported

*** This bug has been marked as a duplicate of bug 43452 ***
Comment 4 Teodor Petrov 2014-04-17 16:27:26 UTC
@Jonathan Wakely: Do you think the ISO C++ standard people will be willing to change this behaviour for a future standard? I'm asking in order to know if there is any point in starting a conversation with them.
Comment 5 Jonathan Wakely 2014-04-17 16:38:54 UTC
That change would force compilers to reject currently valid programs that have well-defined behaviour. Changes that like are not popular.

It is valid in C to pass a pointer to an incomplete type to free(), and this C++ rule is compatible. You can delete incomplete types if they have trivial destructors (like C structs).
Comment 6 Chris Wilson 2016-12-22 22:22:34 UTC
I disagree with the assessment of this bug as a duplicate of bug 43452. That bug was resolved by the creation of the -Wdelete-incomplete option, upon which this bug depends. But this one requests the ability to make this warning into a fatal error, i.e. the creation of a -Werror=delete-incomplete option, which does not exist yet.

Please could you consider reopening this bug and marking it as not a duplicate?
Comment 7 Jonathan Wakely 2016-12-23 02:11:18 UTC
(In reply to Chris Wilson from comment #6)
> I disagree with the assessment of this bug as a duplicate of bug 43452. That
> bug was resolved by the creation of the -Wdelete-incomplete option, upon
> which this bug depends. But this one requests the ability to make this
> warning into a fatal error, i.e. the creation of a -Werror=delete-incomplete
> option, which does not exist yet.

Of course it exists. -Werror=foo is valid for any -Wfoo warning, and it's trivial to check that it works.