From fd08ccd2466e64d5de0ab2a8aaa4455a74fe9eb0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 14 Dec 2010 18:43:49 +0000 Subject: [PATCH] Make wxRefCounter non copyable. wxRefCounter copy ctor was wrong as the new object had the same reference count as the old one instead of starting its life with reference count set to 1 as any new object should. While we could fix its copy ctor, it seems to be better to forbid copying wxRefCounter objects at all because the semantics of doing this is not obvious and the compiler-generated copy ctor in the derived classes often doesn't do what the code using it expects it to do, as was discovered by making wxRefCounter non copyable: see the fixes in the previous commits. To uncover all such bugs, make wxRefCounter and classes deriving from it non copyable. If this uncovers more problems, they should be fixed by implementing copying properly (and explicitly) in the derived classes. Closes #12768. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66374 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gdiobj.h | 7 +++++++ include/wx/msw/gdiimage.h | 2 +- include/wx/object.h | 6 ++++++ src/common/iconbndl.cpp | 12 +++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/wx/gdiobj.h b/include/wx/gdiobj.h index a7f946d2d6..d38a44bb7e 100644 --- a/include/wx/gdiobj.h +++ b/include/wx/gdiobj.h @@ -23,9 +23,16 @@ class WXDLLIMPEXP_CORE wxGDIRefData : public wxObjectRefData { public: + // Default ctor which needs to be defined just because we use + // wxDECLARE_NO_COPY_CLASS() below. + wxGDIRefData() { } + // override this in the derived classes to check if this data object is // really fully initialized virtual bool IsOk() const { return true; } + +private: + wxDECLARE_NO_COPY_CLASS(wxGDIRefData); }; // ---------------------------------------------------------------------------- diff --git a/include/wx/msw/gdiimage.h b/include/wx/msw/gdiimage.h index 10810cf42b..a2016597c2 100644 --- a/include/wx/msw/gdiimage.h +++ b/include/wx/msw/gdiimage.h @@ -40,7 +40,7 @@ public: m_handle = 0; } - wxGDIImageRefData(const wxGDIImageRefData& data) : wxGDIRefData(data) + wxGDIImageRefData(const wxGDIImageRefData& data) : wxGDIRefData() { m_width = data.m_width; m_height = data.m_height; diff --git a/include/wx/object.h b/include/wx/object.h index 80fbae4d2b..5c14734fdd 100644 --- a/include/wx/object.h +++ b/include/wx/object.h @@ -422,6 +422,12 @@ protected: private: // our refcount: int m_count; + + // It doesn't make sense to copy the reference counted objects, a new ref + // counter should be created for a new object instead and compilation + // errors in the code using wxRefCounter due to the lack of copy ctor often + // indicate a problem, e.g. a forgotten copy ctor implementation somewhere. + wxDECLARE_NO_COPY_CLASS(wxRefCounter); }; // ---------------------------------------------------------------------------- diff --git a/src/common/iconbndl.cpp b/src/common/iconbndl.cpp index e87312bc5e..b59a47864e 100644 --- a/src/common/iconbndl.cpp +++ b/src/common/iconbndl.cpp @@ -42,7 +42,17 @@ IMPLEMENT_DYNAMIC_CLASS(wxIconBundle, wxGDIObject) class WXDLLEXPORT wxIconBundleRefData : public wxGDIRefData { public: - // default and copy ctors and assignment operators are ok + wxIconBundleRefData() { } + + // We need the copy ctor for CloneGDIRefData() but notice that we use the + // base class default ctor in it and not the copy one which it doesn't have. + wxIconBundleRefData(const wxIconBundleRefData& other) + : wxGDIRefData(), + m_icons(other.m_icons) + { + } + + // default assignment operator and dtor are ok virtual bool IsOk() const { return !m_icons.empty(); } -- 2.45.2