diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-06-27 13:25:37 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-06-27 17:22:54 +0000 |
commit | 556cdac9ac35eebfb1eab60cac4830679e6fa10a (patch) | |
tree | ea6434f995d902326e03561f794b6faa29a90f4c /src/corelib | |
parent | b3fc0443a085a1c2ad9953448e1234791dc50448 (diff) |
QSharedPointer: destroy LHS on move assignment
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 3 |
1 files changed, 2 insertions, 1 deletions
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 |