[wx-dev] wxString(s.c_str()) thread safety (was Re: wxString threadsafe)

Jan van Dijk jan at epgmod.phys.tue.nl
Wed Apr 23 02:19:13 PDT 2008


On Tuesday 22 April 2008 21:39:35 Vadim Zeitlin wrote:
> On Tue, 22 Apr 2008 20:33:34 +0200 Robert Roebling wrote:
...
>
> RR> > I think we need some wxString method for explicitly making a new copy
> of RR> > the string -- what about wxString::Clone() or
> wxString::DeepCopy()? RR> >
> RR> > Of course, wxString(wxCStrData) ctor should be modified to make a
> deep RR> > copy too, because "wxString(s.c_str())" is the recommended
> idiom for RR> > thread safety copying of wxStrings and not preserving this
> property RR> > would subtly and silently break thread apps.
> RR>
> RR> I agree with all of this,
>
>  Yes, me too (unfortunately).
>

How important is the reference-counting facility anyway? Coudn't we just do 
without? Are there any apps out there that would be seriously pessimized if 
ref-counting would be removed (apart from the poorly written ones that pass 
string-values as arguments, rather than (constant) references)?

Is the present thread another indication that wxString may be getting a tad 
too complex?

I, for starters, would benefit most from a wxString class that behaves as 
similar to std::basic_string<T> as possible. No unnecessary surprises, no 
need to learn more about wxString than necessary. And in the non-unicode 
case, for all practical purposes wxString would be equal to 
std::string<char>.

As an example: our code uses wxWidgets to implement an (optional) graphical 
front-end to our otherwise wx-independent code. As a result, we have 
implemented some code (helper functions, typically) to accept 
std::basic_string<T> as well as wxString. All deviations between wxString and 
std::basic_string will make it more difficult to write code that works with 
wxString as well as with 'legacy' code that is STL-string based (and avoids 
unnecessary conversions between both string types at the same time). 

IF functionality like DeepCopy/Clone is really needed, could this be 
implemented in the fom of free functions? Overloads of these can be easily 
added for std::basic_string<T>. That will greatly improve the 
inter-operability of wxString and std::basic_string<T>.

For the same reasons, I am not happy with the presence or addition of member 
functions to wxString that do not also appear in basic_string. What I said 
above about Clone/DeepCopy also applies to other wxString-members like 
Printf, Format. Apart from backward-compatibility reasons, these may better 
have been implemented as free functions as well... or as both, so I can at 
least choose to avoid using non-STL string member functions in my client 
code.

With kind regards,

	Jan van Dijk.


More information about the wx-dev mailing list