public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/15214] New: Warning non-virtual-dtor too strict
@ 2004-04-30  0:35 tmarshall at real dot com
  2004-04-30  0:36 ` [Bug c++/15214] " pinskia at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: tmarshall at real dot com @ 2004-04-30  0:35 UTC (permalink / raw)
  To: gcc-bugs

It seems to me that it is safe to have virtual functions and a non-virtual dtor
if the dtor can only be invoked by methods within the class.  That is, the dtor
must be private and the class must not declare any friends.

Sample code:

#include <stdio.h>

class A
{
public:
    A() : m_ref(0) { printf("a::ctor\n"); }

    virtual int addref(void);
    virtual int release(void);

private:
    ~A() { printf("a::dtor\n"); }

protected:
    int m_ref;
};

int A::addref(void)
{
    return ++m_ref;
}
int A::release(void)
{
    int ref = --m_ref;
    if (ref == 0) delete this;
    return ref;
}
 
int main(void)
{
    A* a = new A;
    a->addref();
    a->release();
    return 0;
}

Patch:

--- class.c.orig        2004-04-29 16:51:53.000000000 -0700
+++ class.c     2004-04-29 16:51:55.000000000 -0700
@@ -5102,7 +5102,17 @@
 
   if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
-    warning ("`%#T' has virtual functions but non-virtual destructor", t);
+    {
+      tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
+
+      /* Warn only if the dtor is non-private or the class has friends */
+      if (!TREE_PRIVATE (dtor) ||
+         (CLASSTYPE_FRIEND_CLASSES (t) ||
+          DECL_FRIENDLIST (TYPE_MAIN_DECL (t))))
+       {
+         warning ("%#T' has virtual functions but non-virtual destructor", t);
+       }
+    }
 
   complete_vars (t);

-- 
           Summary: Warning non-virtual-dtor too strict
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tmarshall at real dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
@ 2004-04-30  0:36 ` pinskia at gcc dot gnu dot org
  2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-30  0:36 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-04-30 00:26 -------
Even what you are proposing is too strict, see PR 7302 (I am going to close that one as a dup of this 
one though).

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
           Keywords|                            |diagnostic
   Last reconfirmed|0000-00-00 00:00:00         |2004-04-30 00:26:57
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
  2004-04-30  0:36 ` [Bug c++/15214] " pinskia at gcc dot gnu dot org
  2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
@ 2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
  2004-04-30  5:52 ` tmarshall at real dot com
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-30  0:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-04-30 00:35 -------
I think you can just change !TREE_PRIVATE to be TREE_PUBLIC and submit it to gcc-
patches@gcc.gnu.org with a changelog and ___AFTER___ reading <http://gcc.gnu.org/
contribute.html>.  If you cannot make testcases just ask for help with them.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
  2004-04-30  0:36 ` [Bug c++/15214] " pinskia at gcc dot gnu dot org
@ 2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
  2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-30  0:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-04-30 00:31 -------
*** Bug 7302 has been marked as a duplicate of this bug. ***

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cesarb at nitnet dot com dot
                   |                            |br


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (2 preceding siblings ...)
  2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
@ 2004-04-30  5:52 ` tmarshall at real dot com
  2004-05-12 15:26 ` bangerth at dealii dot org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: tmarshall at real dot com @ 2004-04-30  5:52 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tmarshall at real dot com  2004-04-30 03:45 -------
(In reply to comment #3)
> I think you can just change !TREE_PRIVATE to be TREE_PUBLIC and submit it to gcc-
> patches@gcc.gnu.org with a changelog and ___AFTER___ reading <http://gcc.gnu.org/
> contribute.html>.  If you cannot make testcases just ask for help with them.

I don't think this is correct either.  This would make it possible to declare
class A with virtual functions and a protected non-virtual dtor, then declare
class B that derives from A, also with a protected non-virtual dtor, without
invoking the warning.  I am not familiar enough with the internals of GCC to
know whether there is an easy solution for bug 7302.  In fact, I think that it
is just a corner case that can probably be marked WONTFIX.  If you declare a
class in which all functions are pure virtual (including all base class(es)),
what sense is there in providing an explicit dtor?  If it is to cleanup member
variables, the dtor surely must be virtual to ensure it is properly invoked,
correct?

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (3 preceding siblings ...)
  2004-04-30  5:52 ` tmarshall at real dot com
@ 2004-05-12 15:26 ` bangerth at dealii dot org
  2004-05-12 15:28 ` bangerth at dealii dot org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2004-05-12 15:26 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-05-11 18:38 -------
A patch was submitted here: 
  http://gcc.gnu.org/ml/gcc-patches/2004-05/msg00599.html 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (4 preceding siblings ...)
  2004-05-12 15:26 ` bangerth at dealii dot org
@ 2004-05-12 15:28 ` bangerth at dealii dot org
  2004-05-12 15:47 ` tmarshall at real dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2004-05-12 15:28 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-05-11 18:53 -------
I don't quite understand the usefulness of the construct you want the  
compiler to accept: if the destructor can't be called from a derived  
class, then you can derive from this class. Why would you want to have  
virtual functions then?  
  
W.  

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (5 preceding siblings ...)
  2004-05-12 15:28 ` bangerth at dealii dot org
@ 2004-05-12 15:47 ` tmarshall at real dot com
  2004-05-12 16:35 ` bangerth at dealii dot org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: tmarshall at real dot com @ 2004-05-12 15:47 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tmarshall at real dot com  2004-05-11 19:03 -------
(In reply to comment #6)
> I don't quite understand the usefulness of the construct you want the  
> compiler to accept: if the destructor can't be called from a derived  
> class, then you can derive from this class. Why would you want to have  
> virtual functions then?  

The class may derive from base class(es) that have virtual functions.  This is
a common construct in COM programming, for example.  The class implements one or
more interfaces (class definitions that consist of pure virtual functions) and
uses "delete this" in its Release() method.  Making the dtor private helps to
ensure that it is being used properly.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (6 preceding siblings ...)
  2004-05-12 15:47 ` tmarshall at real dot com
@ 2004-05-12 16:35 ` bangerth at dealii dot org
  2004-05-29  0:32 ` cvs-commit at gcc dot gnu dot org
  2004-05-29  1:24 ` pinskia at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2004-05-12 16:35 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-05-11 19:23 -------
Ah, ok. Basically what you do is to mark the class "final", i.e. no 
other class can derive from it. I think your claim then makes sense. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (7 preceding siblings ...)
  2004-05-12 16:35 ` bangerth at dealii dot org
@ 2004-05-29  0:32 ` cvs-commit at gcc dot gnu dot org
  2004-05-29  1:24 ` pinskia at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu dot org @ 2004-05-29  0:32 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From cvs-commit at gcc dot gnu dot org  2004-05-28 17:01 -------
Subject: Bug 15214

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jason@gcc.gnu.org	2004-05-28 17:01:21

Modified files:
	gcc/cp         : ChangeLog class.c 

Log message:
	PR c++/15214
	* class.c (finish_struct_1): Warn only if the dtor is non-private or
	the class has friends.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4060&r2=1.4061
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/class.c.diff?cvsroot=gcc&r1=1.614&r2=1.615



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/15214] Warning non-virtual-dtor too strict
  2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
                   ` (8 preceding siblings ...)
  2004-05-29  0:32 ` cvs-commit at gcc dot gnu dot org
@ 2004-05-29  1:24 ` pinskia at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-05-29  1:24 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-05-28 17:13 -------
Fixed.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |3.5.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15214


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-05-28 17:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-30  0:35 [Bug c++/15214] New: Warning non-virtual-dtor too strict tmarshall at real dot com
2004-04-30  0:36 ` [Bug c++/15214] " pinskia at gcc dot gnu dot org
2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
2004-04-30  0:41 ` pinskia at gcc dot gnu dot org
2004-04-30  5:52 ` tmarshall at real dot com
2004-05-12 15:26 ` bangerth at dealii dot org
2004-05-12 15:28 ` bangerth at dealii dot org
2004-05-12 15:47 ` tmarshall at real dot com
2004-05-12 16:35 ` bangerth at dealii dot org
2004-05-29  0:32 ` cvs-commit at gcc dot gnu dot org
2004-05-29  1:24 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).