From b96a56e60e65e17704fc66ae9cdbb2444ac987d8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 28 Feb 2010 11:09:26 +0000 Subject: [PATCH] Make it possible to use wxCharBuffer during program initialization. wxCharBuffer might be used during static initialization, even if only implicitly. E.g. it is used by wxString::Format() which can be used to initialize a global string. But it uses the global s_untypedNullData variable might not be initialized yet resulting in mysterious failures. Fix this in the usual way by wrapping access to the variable via a function. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63585 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/buffer.h | 8 ++------ src/common/string.cpp | 16 ++++++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 864bb64eb7..4be2660b67 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -56,12 +56,8 @@ struct UntypedBufferData bool m_owned; }; -// this has to be defined inside the DLL (and not e.g. as a static variable -// inside an inline function) as otherwise MSVC gives link errors when the -// functions are effectively inlined (i.e. in non-debug build) -// // NB: this is defined in string.cpp and not the (non-existent) buffer.cpp -extern WXDLLIMPEXP_DATA_BASE(UntypedBufferData * const) untypedNullDataPtr; +WXDLLIMPEXP_BASE UntypedBufferData * GetUntypedNullData(); } // namespace wxPrivate @@ -186,7 +182,7 @@ protected: // placeholder for NULL string, to simplify this code static Data *GetNullData() { - return static_cast(wxPrivate::untypedNullDataPtr); + return static_cast(wxPrivate::GetUntypedNullData()); } void IncRef() diff --git a/src/common/string.cpp b/src/common/string.cpp index 9e9ba528fc..dcac014916 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -57,16 +57,20 @@ #define wxStringStrlen wxStrlen #endif -// ---------------------------------------------------------------------------- -// global variables -// ---------------------------------------------------------------------------- - +// define a function declared in wx/buffer.h here as we don't have buffer.cpp +// and don't want to add it just because of this simple function namespace wxPrivate { -static UntypedBufferData s_untypedNullData(NULL, 0); +// wxXXXBuffer classes can be (implicitly) used during global statics +// initialization so wrap the status UntypedBufferData variable in a function +// to make it safe to access it even before all global statics are initialized +UntypedBufferData *GetUntypedNullData() +{ + static UntypedBufferData s_untypedNullData(NULL, 0); -UntypedBufferData * const untypedNullDataPtr = &s_untypedNullData; + return &s_untypedNullData; +} } // namespace wxPrivate