[wx-dev] return of wxString::utf8_str()

Robert Roebling robert at roebling.de
Tue Jul 3 12:30:03 PDT 2007


Vaclav Slavik wrote:
> Vadim Zeitlin wrote:
> > RR> I just saw code in the GNOME printing backend
> > RR> that does this
> > RR>
> > RR> const char *utf8 = string.utf8_str();
> > RR>
> > RR> and it then uses utf8. How do we know that the string
> > RR> isn't destroyed afterwards
> >
> >  It isn'tin UTF-8 build (where utf8_str() just returns the pointer
> > to the internal representation) but it's definitely a bug in
> > wchar_t Unicode build or ANSI build. 
> 
> No, it isn't, it's just Robert quoting single line of code out of 
> context:
> 
> // FIXME-UTF8: wouldn't be needed if utf8_str() always returned a 
> // buffer
> #if wxUSE_UNICODE_UTF8
>     const char *data = text.utf8_str();

I don't think it has anything to do with the context. The 
wxString instance "text" above is not used anywhere after
that line so the compiler *could* destroy it right there.
In that case "data" would point to rubbish. As I said,
the "text" object probably is destroyed later. 

The code above would work like this:

Foo( text.utf8_str() );  // correct

but it is like this:

const char *data = text.utf8_str()
// text could be destroyed here
Foo( data );             // not correct

  Robert






More information about the wx-dev mailing list