From fe3e83e64892d5325fe83381447eaf1ef4c0e623 Mon Sep 17 00:00:00 2001 From: Eisenwave Date: Sun, 25 Jun 2023 05:40:24 +0200 Subject: [PATCH] fix memory leak in example of C.66 (#2096) --- CppCoreGuidelines.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 5faec17..3b89296 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -6436,8 +6436,13 @@ A non-throwing move will be used more efficiently by standard-library and langua template class Vector { public: - Vector(Vector&& a) noexcept :elem{a.elem}, sz{a.sz} { a.sz = 0; a.elem = nullptr; } - Vector& operator=(Vector&& a) noexcept { elem = a.elem; sz = a.sz; a.sz = 0; a.elem = nullptr; } + Vector(Vector&& a) noexcept :elem{a.elem}, sz{a.sz} { a.elem = nullptr; a.sz = 0; } + Vector& operator=(Vector&& a) noexcept { + delete elem; + elem = a.elem; a.elem = nullptr; + sz = a.sz; a.sz = 0; + return *this; + } // ... private: T* elem;