From 79b5ab70dd0fd886758b9bcee666cc7ca1748a41 Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Thu, 20 Feb 2020 21:40:59 -0500 Subject: [PATCH] Add -Wnon-virtual-dtor and virtual destructors This warning detects classes with virtual functions but a non-virtual destructor. Deleting instances through pointers whose type is such a class is undefined behaviour. I'm not certain whether any of the classes I've added the virtual destructors to here actually needed it, but there should be negligible performance impact, and it's a good warning to have enabled. --- CMakeLists.txt | 1 + Makefile | 1 + src/behavior_strategy.h | 1 + src/inventory_ui.h | 3 ++- src/item_stack.h | 1 + 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c67c08024c545..4eaf719eeeb9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -225,6 +225,7 @@ ELSE() -Wlogical-op \ -Wmissing-declarations \ -Wmissing-noreturn \ + -Wnon-virtual-dtor \ -Wold-style-cast \ -Woverloaded-virtual \ -Wpedantic \ diff --git a/Makefile b/Makefile index 8040db98917a2..5de3e461e12ce 100644 --- a/Makefile +++ b/Makefile @@ -93,6 +93,7 @@ WARNINGS = \ -Wlogical-op \ -Wmissing-declarations \ -Wmissing-noreturn \ + -Wnon-virtual-dtor \ -Wold-style-cast \ -Woverloaded-virtual \ -Wpedantic \ diff --git a/src/behavior_strategy.h b/src/behavior_strategy.h index b5a374d67d76d..587367dddb7b8 100644 --- a/src/behavior_strategy.h +++ b/src/behavior_strategy.h @@ -18,6 +18,7 @@ struct behavior_return; class strategy_t { public: + virtual ~strategy_t() = default; virtual behavior_return evaluate( const oracle_t *subject, std::vector children ) const = 0; }; diff --git a/src/inventory_ui.h b/src/inventory_ui.h index f1afe1ad7245e..331f4e2d21a2f 100644 --- a/src/inventory_ui.h +++ b/src/inventory_ui.h @@ -129,6 +129,7 @@ class inventory_selector_preset { public: inventory_selector_preset(); + virtual ~inventory_selector_preset() = default; /** Does this entry satisfy the basic preset conditions? */ virtual bool is_shown( const item_location & ) const { @@ -424,7 +425,7 @@ class inventory_selector { public: inventory_selector( player &u, const inventory_selector_preset &preset = default_preset ); - ~inventory_selector(); + virtual ~inventory_selector(); /** These functions add items from map / vehicles. */ void add_character_items( Character &character ); void add_map_items( const tripoint &target ); diff --git a/src/item_stack.h b/src/item_stack.h index 6df19649b0c6c..58da76cbf0c60 100644 --- a/src/item_stack.h +++ b/src/item_stack.h @@ -28,6 +28,7 @@ class item_stack using const_reverse_iterator = cata::colony::const_reverse_iterator; item_stack( cata::colony *items ) : items( items ) { } + virtual ~item_stack() = default; size_t size() const; bool empty() const;