From 45f6e3521a2b4f535e29d03dd077978a691bdb58 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Sat, 9 Mar 2024 22:05:50 -0800 Subject: finally.hh: delete copy constructor which is a bad idea Change-Id: I6d0b5736893c44bddc6f5789b452b434f8671b9b --- src/libutil/finally.hh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh index db654301f..49263ee6d 100644 --- a/src/libutil/finally.hh +++ b/src/libutil/finally.hh @@ -9,8 +9,15 @@ class Finally { private: Fn fun; + bool movedFrom = false; public: Finally(Fn fun) : fun(std::move(fun)) { } - ~Finally() { fun(); } + // Copying Finallys is definitely not a good idea and will cause them to be + // called twice. + Finally(Finally &other) = delete; + Finally(Finally &&other) : fun(std::move(other.fun)) { + other.movedFrom = true; + } + ~Finally() { if (!movedFrom) fun(); } }; -- cgit v1.2.3