On Thu, May 04, 2006 at 11:16:46PM +0200, Edwin Leuven wrote:
> Andre Poenitz wrote:
> >On Thu, May 04, 2006 at 10:05:41AM +0200, Edwin Leuven wrote:
> >>- Q3CString tmpstr = codec->fromUnicode(str);
> >>- char const * tmpcstr = tmpstr;
> >>+ char const * tmpcstr = codec->fromUnicode(str).data();
> >> return tmpcstr[0];
> >> }
> >
> >fromUnicode() returns a temporary object that will be destoyed at the
> >end of the full expression. So tmpcstr will be invalid after that.
> >
> >Accessing tmpcstr[0] after that will cause invalid behaviour (and almost
> >guaranteed a crash with the Qt4 equivalent on a VS 2005 release build).
>
> und jetzt?
Either
[Let 'Foo' be whatever 'fromUnicode returns]
Foo tmpstr = codec->fromUnicode(str);
return tmpstr.data()[0];
or
Foo const & tmpstr = codec->fromUnicode(str);
return tmpstr.data()[0];
or
return codec->fromUnicode(str).data()[0];
In any case, the temporaries must be alive until the []
is executed.
Andre'
[PS: It's not a 'const' issue, Abdel]