From 556cdac9ac35eebfb1eab60cac4830679e6fa10a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 27 Jun 2015 13:25:37 +0200 Subject: [PATCH] QSharedPointer: destroy LHS on move assignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Howard Hinnant is right: just swapping may keep a resource alive too long. The problem with replacing swap(other) was that the naïve approach: clear(); swap(other); is not safe for self-assignment. Taking a cue from the default std::swap() implementation, and the copy-swap idiom, a self-assignment-safe version is QSharedPointer moved(std::move(other)); swap(moved); which is to what I changed the implementation now. Change-Id: I589fdae50ae22b95350db8250b02d983dc8487a6 Reviewed-by: Thiago Macieira --- src/corelib/tools/qsharedpointer_impl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 3d20f4dca9..50b3bcf0c4 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -332,7 +332,8 @@ public: } inline QSharedPointer &operator=(QSharedPointer &&other) { - swap(other); + QSharedPointer moved(std::move(other)); + swap(moved); return *this; } #endif