summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-10-18 13:58:19 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-10-18 14:23:21 -0700
commitd7e5119b4c64a960ae66fdc0478e9658c9ebbf63 (patch)
tree2bc4f78a33c449272fffc94e21dbca9b5ce4ae03
parent8eea85618c4a070fda6257f2be5280f9684c93c6 (diff)
downloadtmwa-d7e5119b4c64a960ae66fdc0478e9658c9ebbf63.tar.gz
tmwa-d7e5119b4c64a960ae66fdc0478e9658c9ebbf63.tar.bz2
tmwa-d7e5119b4c64a960ae66fdc0478e9658c9ebbf63.tar.xz
tmwa-d7e5119b4c64a960ae66fdc0478e9658c9ebbf63.zip
Fix bug where Option destroys too much
-rw-r--r--src/compat/option.hpp5
-rw-r--r--src/compat/option_test.cpp35
2 files changed, 39 insertions, 1 deletions
diff --git a/src/compat/option.hpp b/src/compat/option.hpp
index 27ee0bc..1976589 100644
--- a/src/compat/option.hpp
+++ b/src/compat/option.hpp
@@ -171,7 +171,10 @@ namespace option
}
~Option()
{
- do_destruct();
+ if (repr.is_some())
+ {
+ do_destruct();
+ }
}
T move_or(T def)
diff --git a/src/compat/option_test.cpp b/src/compat/option_test.cpp
index b963a29..ac95424 100644
--- a/src/compat/option_test.cpp
+++ b/src/compat/option_test.cpp
@@ -117,6 +117,41 @@ TEST(Option, customrepr)
}
}
+TEST(Option, destruct)
+{
+ struct BugCheck
+ {
+ bool *destroyed;
+
+ BugCheck(bool *d)
+ : destroyed(d)
+ {}
+ BugCheck(BugCheck&& r)
+ : destroyed(r.destroyed)
+ {
+ r.destroyed = nullptr;
+ }
+ BugCheck& operator = (BugCheck&& r)
+ {
+ std::swap(destroyed, r.destroyed);
+ return *this;
+ }
+ ~BugCheck()
+ {
+ if (!destroyed)
+ return;
+ if (*destroyed)
+ abort();
+ *destroyed = true;
+ }
+ };
+
+ bool destroyed = false;
+
+ Option<BugCheck> bug = Some(BugCheck(&destroyed));
+ bug = None;
+}
+
TEST(Option, def)
{
struct Tracked