Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weak vtables warnings #3418

Merged
merged 2 commits into from
Jan 24, 2025
Merged

Conversation

bernardnormier
Copy link
Member

With clang, a class without any out-of-line virtual function has a "weak vtable", meaning clang stores this vtable in all translation units.

I believe this is an issue only with shared libraries, where having 2 vtables for the same class in different objects (say exe and shared library) leads to dynamic_cast and catch(const exception&) failures at runtime.

This PR addresses this issue by turning on -Wweak-vtables and fixing many weak vtables warnings in shared libraries.

The usual fix is to define the virtual destructor out of line:

MyClass::~MyClass() = default;

It's only meaningful when MyClass is exported (= part of the public API, or the semi-public API that plugins can use). And putting a virtual function out of line doesn't work with template classes.

Fixes #3409.

@@ -5,5 +5,6 @@ $(project)_libraries := IceStorm
IceStorm_targetdir := $(libdir)
IceStorm_dependencies := Ice
IceStorm_sliceflags := --include-dir IceStorm
IceStorm_cppflags := $(api_exports_cppflags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no -DICESTORM_API_EXPORTS because all the code in this library is generated code.

{
public:
ICESTORM_SERVICE_API static std::shared_ptr<Service> create(
~Service() override;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the only relevant header in IceStormService - the only exported class.

# lmdb is not necessary for the icestormadmin sources. However, we want to build all objects with the same flags to
# reuse common object files in the different programs.
# we also include api_exports_cppflags because we "export" IceStormService to IceGrid.
$(project)_cppflags := $(if $(lmdb_includedir),-I$(lmdb_includedir)) $(api_exports_cppflags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to use the same cppflags for all sources in each project.

@@ -5,5 +5,6 @@ $(project)_libraries := IceGrid
IceGrid_targetdir := $(libdir)
IceGrid_dependencies := Glacier2 Ice
IceGrid_sliceflags := --include-dir IceGrid
IceGrid_cppflags := -DICEGRID_API_EXPORTS $(api_exports_cppflags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-DICEGRID_API_EXPORTS was missing. It doesn't really matter with gcc/clang since IMPORTS/EXPORTS expand to the same... but it's clearer.

@@ -4,6 +4,7 @@ $(project)_libraries := IceBox
$(project)_programs := icebox iceboxadmin
$(project)_dependencies := Ice
$(project)_sliceflags := --include-dir IceBox
$(project)_cppflags := $(api_exports_cppflags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Makefile.mk is not great as it produces both the IceBox library and the IceBox exe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set per component flags too, something like

# For icebox exe 
icebox_cppflags := ...

# For iceboxadmin exe
iceboxadmin_cppflags := ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but this conflicts with the shared object folder / object reuse per project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this apply here, the icebox exe objects should not be used anywhere else.

@@ -29,13 +29,16 @@ namespace IceInternal
class CancellationHandler
{
public:
virtual ~CancellationHandler();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary - since not exported - but simpler than suppressing the warning via a pragma.

@bernardnormier bernardnormier merged commit fdb0f14 into zeroc-ice:main Jan 24, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Review MAXWARN setting in config/Make.rules.Darwin
3 participants