Opened 12 years ago

Closed 10 years ago

#4200 closed Bugs (fixed)

Virtual functions, non-virtual destructor

Reported by: klimkin@… Owned by: Emil Dotchevski
Milestone: To Be Determined Component: exception
Version: Boost 1.48.0 Severity: Problem
Keywords: Cc: GCC 4.3.2

Description

Non-virtual destructor produces warnings, like: warning: `boost::error_info<cm::ErrnoTag, int>' has virtual functions but non-virtual destructor

Attachments (1)

boost_exception_warning.patch (407 bytes ) - added by klimkin@… 12 years ago.
Patch for error_info_impl.hpp

Download all attachments as: .zip

Change History (19)

by klimkin@…, 12 years ago

Patch for error_info_impl.hpp

comment:1 by anonymous, 12 years ago

Resolution: worksforme
Status: newclosed

I don't have GCC 4.3.2 installed, but with -Wall I don't see any warnings using GCC 4.4.1 with the latest trunk. Please provide exact command line and source code.

comment:2 by Jan Kundrát <jkt@…>, 12 years ago

Resolution: worksforme
Status: closedreopened

With gcc, you have to build with -Weffc++ in order to see warnings about non-virtual destructors in base class. For the record, I'm gettting this warning when using boost::spirit::qi::grammar.

comment:3 by Emil Dotchevski, 12 years ago

GCC 4.5.0/MinGW with -Weffc++ fails to produce this warning for me. Please provide exact command line for a single cpp file in libs/exception/test which produces the warning.

comment:4 by federico.schwindt@…, 12 years ago

Try with -Wnon-virtual-dtor.

comment:5 by Emil Dotchevski, 12 years ago

Resolution: worksforme
Status: reopenedclosed

I've tried with various command line options. I need *exact* compiler version and *exact* command line that shows the warning.

comment:6 by federico.schwindt@…, 12 years ago

I get the error compiling mongodb with gcc 4.2.1 as it uses -Wnon-virtual-dtor (which is also enabled if -Weffc++ is used as per gcc manpage).

comment:7 by anonymous, 11 years ago

Milestone: Boost 1.43.0To Be Determined
Resolution: worksforme
Status: closedreopened
Version: Boost 1.43.0Boost 1.48.0

Can reproduce in 1.48.0 with:

Apple clang version 3.0 (tags/Apple/clang-211.10.1) (based on LLVM 3.0svn) Target: x86_64-apple-darwin11.2.0 Thread model: posix

I'm not smart enough to make a test case.

The patch worked for me.

comment:8 by shartwell@…, 11 years ago

Here's a test case for gcc. Also applies to all versions of clang.

$ cat nonvdtor.cpp 
#include <string>
class
error_info_base
   {
   public:
   virtual std::string tag_typeid_name() const = 0;
   virtual std::string value_as_string() const = 0;
   protected:
   ~error_info_base() throw()
       {
       }
   };
$ gcc -v -c -Wnon-virtual-dtor nonvdtor.cpp
...
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)
...
nonvdtor.cpp:5: warning: ‘class error_info_base’ has virtual functions
     but non-virtual destructor

Declaring ~error_info_base() virtual will avoid the warning and allow our sources to include boost headers without having to suppress this warning in our own code.

-- Steve Hartwell

comment:9 by Emil Dotchevski, 11 years ago

Maybe I'm missing something, but I get no warnings running the Boost Exception tests with the command line:

bjam variant=debug toolset=gcc cxxflags=-Wnon-virtual-dtor

Same if I use:

bjam variant=debug toolset=gcc cxxflags=-Weffc++

Also, here's my compiler version:

$ g++ --version
g++.exe (GCC) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Is there any reason why you pulled the code out from the Boost Exception header rather than just including it?

comment:10 by Mathias Gaunard, 11 years ago

I am getting tons of related errors, some of which come from using exception_ptr (which uses shared_ptr and checked_delete under the hood) to transfer exceptions between threads.

warning: deleting object of polymorphic class type `boost::error_info<boost::tag_original_exception_type, const std::type_info*>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor].

This happens with -Wall on GCC 4.7.

The fix is trivial, please just include it.

comment:11 by Emil Dotchevski, 11 years ago

I just tried the following command line with GCC 4.7 on Windows:

....\trunk\libs\exception\test>g++ -c -I../../.. -Wall error_info_test.cpp

The above command does not generate any console output.

My GCC version:

....\trunk\libs\exception\test>g++ --version
g++ (GCC) 4.7.0
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I do want to prevent this warning, but I still need help reproducing it.

comment:12 by zeratul976@…, 10 years ago

Emil, rather than compiling error_info_test.cpp, make a new file "test.cpp" in the same directory, with the following line as its content:

#include <boost/thread/future.hpp>

and try to compile it with the same options. You will now see the warning.

comment:13 by anonymous, 10 years ago

source code(a.cc):

#include <boost/exception/all.hpp>

typedef boost::error_info<struct my_info_tag, int> my_info;

int main()
{
        my_info(0);
}

compiler: gcc-3.4.6

command line: g++ -Wall a.cc

compiler output:

/usr/local/include/boost/exception/detail/error_info_impl.hpp: In instantiation of `boost::error_info<my_info_tag, int>':
a.cc:7:   instantiated from here
/usr/local/include/boost/exception/detail/error_info_impl.hpp:43: warning: `class boost::error_info<my_info_tag, int>' has virtual functions but non-virtual destructor

comment:14 by Dean Michael Berris, 10 years ago

Any update on this? Can we please just merge this and be done with it?

comment:15 by persgray@…, 10 years ago

FWIW, the "virtual" modificator was removed in that commit:

r58072 | emildotchevski | 2009-12-01 05:16:50 +0300 (Tue, 01 Dec 2009) | 1 line

Suppressing warnings. Please report any problems (may have broken something!)

So this really broke all software using boost and -Werror. Emil, what's were the warnings that r58072 should be fixing?

comment:16 by Emil Dotchevski, 10 years ago

The commit you mention is from 4 years ago. As far as I can tell virtual is present currently.

in reply to:  16 comment:17 by viboes, 10 years ago

Replying to emildotchevski:

The commit you mention is from 4 years ago. As far as I can tell virtual is present currently.

Emil please, close this ticket and the other related to this warning if you have fixed them.

comment:18 by Emil Dotchevski, 10 years ago

Resolution: fixed
Status: reopenedclosed
Note: See TracTickets for help on using tickets.