Qt-interest Archive, December 2007
A QString question...
Message 1 in thread
Given the code:
QString testQString(char * input)
{
return QString(input);
}
qint32 main(int argc, char *argv[])
{
qint32 ret = 0;
QtSingleApplication shellApp(SHELL_APP_NAME, argc, argv);
Settings::Instance(&shellApp, SETTINGS_FILE_PREFIX);
LogFile::Instance(SHELL_LOG_FILE_PREFIX);
Debug("UPCShell::main()");
QString test = testQString("spot");
ret = shellApp.exec();
}
Would you expect issues with the return type of the testQString
function. We have many usages of this return type in our code and on
Windows its seems to work fine, but on Mac OS X 10.5.1 using Xcode 3.0
(GCC 4.0.1), we have function like testQString which return garbage
QStrings. Oddly enough, this simple example works fine on the Mac.
Thanks,
-Tom Condon
Message 2 in thread
On 04.12.07 05:32:49, tomcondon@xxxxxxx wrote:
> Given the code:
>
> QString testQString(char * input)
>
> {
> return QString(input);
> }
>
>
> qint32 main(int argc, char *argv[])
>
> {
>
> qint32 ret = 0;
> QtSingleApplication shellApp(SHELL_APP_NAME, argc, argv);
> Settings::Instance(&shellApp, SETTINGS_FILE_PREFIX);
> LogFile::Instance(SHELL_LOG_FILE_PREFIX);
> Debug("UPCShell::main()");
>
>
> QString test = testQString("spot");
> ret = shellApp.exec();
> }
>
> Would you expect issues with the return type of the testQString function.
> We have many usages of this return type in our code and on Windows its
> seems to work fine, but on Mac OS X 10.5.1 using Xcode 3.0 (GCC 4.0.1), we
> have function like testQString which return garbage QStrings. Oddly
> enough, this simple example works fine on the Mac.
Did you try with some literal non-ascii characters? The QString(const
char*) constructor uses fromAscii() in Qt4 (and fromLatin1 in Qt3, IIRC)
to convert the char array into unicode and thus it will fail with
anything but plain ascii. What you probably want is ::fromUtf8, though
you have to make sure all your sources are utf8 encoded and any input
from libraries that use const char* is utf8.
Andreas
--
[ signature omitted ]
Message 3 in thread
That is very good info for us, but what I was trying to get at was
that the returned QString() creates a temporary QString. Is that
going to be a sane value after the function returns. In some of our
cases "return QString("spot")" is returning garbage, but only under
GCC 4.0.1 on the Mac. Visual C++ is fine with it.
On Dec 4, 2007, at 5:51 AM, Andreas Pakulat wrote:
> On 04.12.07 05:32:49, tomcondon@xxxxxxx wrote:
>> Given the code:
>>
>> QString testQString(char * input)
>>
>> {
>> return QString(input);
>> }
>>
>>
>> qint32 main(int argc, char *argv[])
>>
>> {
>>
>> qint32 ret = 0;
>> QtSingleApplication shellApp(SHELL_APP_NAME, argc, argv);
>> Settings::Instance(&shellApp, SETTINGS_FILE_PREFIX);
>> LogFile::Instance(SHELL_LOG_FILE_PREFIX);
>> Debug("UPCShell::main()");
>>
>>
>> QString test = testQString("spot");
>> ret = shellApp.exec();
>> }
>>
>> Would you expect issues with the return type of the testQString
>> function.
>> We have many usages of this return type in our code and on Windows
>> its
>> seems to work fine, but on Mac OS X 10.5.1 using Xcode 3.0 (GCC
>> 4.0.1), we
>> have function like testQString which return garbage QStrings. Oddly
>> enough, this simple example works fine on the Mac.
>
> Did you try with some literal non-ascii characters? The QString(const
> char*) constructor uses fromAscii() in Qt4 (and fromLatin1 in Qt3,
> IIRC)
> to convert the char array into unicode and thus it will fail with
> anything but plain ascii. What you probably want is ::fromUtf8, though
> you have to make sure all your sources are utf8 encoded and any input
> from libraries that use const char* is utf8.
>
> Andreas
>
> --
> Communicate! It can't make things any worse.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
--
[ signature omitted ]
Message 4 in thread
You are returning a copy of the generated QString, as such it should
not return garbage.
What happens in your function is this:
. Construct a temporary QString
. Copy that QString to the return value
. Destruct the temporary QString
If it does return garbage after all you might have hit a compiler bug.
Either that or Qt does not do a deep copy (which I am not sure of
right now, not exactly remembering Qt's copy-on-write mechanism).
- Thomas
2007/12/4, tomcondon@xxxxxxx <tomcondon@xxxxxxx>:
> That is very good info for us, but what I was trying to get at was
> that the returned QString() creates a temporary QString. Is that
> going to be a sane value after the function returns. In some of our
> cases "return QString("spot")" is returning garbage, but only under
> GCC 4.0.1 on the Mac. Visual C++ is fine with it.
> On Dec 4, 2007, at 5:51 AM, Andreas Pakulat wrote:
>
> > On 04.12.07 05:32:49, tomcondon@xxxxxxx wrote:
> >> Given the code:
> >>
> >> QString testQString(char * input)
> >>
> >> {
> >> return QString(input);
> >> }
> >>
> >>
> >> qint32 main(int argc, char *argv[])
> >>
> >> {
> >>
> >> qint32 ret = 0;
> >> QtSingleApplication shellApp(SHELL_APP_NAME, argc, argv);
> >> Settings::Instance(&shellApp, SETTINGS_FILE_PREFIX);
> >> LogFile::Instance(SHELL_LOG_FILE_PREFIX);
> >> Debug("UPCShell::main()");
> >>
> >>
> >> QString test = testQString("spot");
> >> ret = shellApp.exec();
> >> }
> >>
> >> Would you expect issues with the return type of the testQString
> >> function.
> >> We have many usages of this return type in our code and on Windows
> >> its
> >> seems to work fine, but on Mac OS X 10.5.1 using Xcode 3.0 (GCC
> >> 4.0.1), we
> >> have function like testQString which return garbage QStrings. Oddly
> >> enough, this simple example works fine on the Mac.
> >
> > Did you try with some literal non-ascii characters? The QString(const
> > char*) constructor uses fromAscii() in Qt4 (and fromLatin1 in Qt3,
> > IIRC)
> > to convert the char array into unicode and thus it will fail with
> > anything but plain ascii. What you probably want is ::fromUtf8, though
> > you have to make sure all your sources are utf8 encoded and any input
> > from libraries that use const char* is utf8.
> >
> > Andreas
> >
> > --
> > Communicate! It can't make things any worse.
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> > with "unsubscribe" in the subject or the body.
> > List archive and information: http://lists.trolltech.com/qt-interest/
> >
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 5 in thread
Returning a QString in this way from a function definitely works. Your bug must be somewhere else.
Cheers,
Peter
> -----Ursprüngliche Nachricht-----
> Von: Thomas Dähling [mailto:t.daehling@xxxxxxxxxxxxxx]
> Gesendet: Dienstag, 4. Dezember 2007 15:09
> An: qt-interest@xxxxxxxxxxxxx
> Cc: qt-interest@xxxxxxxxxxxxx
> Betreff: Re: A QString question...
>
> You are returning a copy of the generated QString, as such it should
> not return garbage.
>
> What happens in your function is this:
>
> . Construct a temporary QString
> . Copy that QString to the return value
> . Destruct the temporary QString
>
> If it does return garbage after all you might have hit a compiler bug.
> Either that or Qt does not do a deep copy (which I am not sure of
> right now, not exactly remembering Qt's copy-on-write mechanism).
>
> - Thomas
>
> 2007/12/4, tomcondon@xxxxxxx <tomcondon@xxxxxxx>:
> > That is very good info for us, but what I was trying to get at was
> > that the returned QString() creates a temporary QString. Is that
> > going to be a sane value after the function returns. In some of our
> > cases "return QString("spot")" is returning garbage, but only under
> > GCC 4.0.1 on the Mac. Visual C++ is fine with it.
> > On Dec 4, 2007, at 5:51 AM, Andreas Pakulat wrote:
> >
> > > On 04.12.07 05:32:49, tomcondon@xxxxxxx wrote:
> > >> Given the code:
> > >>
> > >> QString testQString(char * input)
> > >>
> > >> {
> > >> return QString(input);
> > >> }
> > >>
> > >>
> > >> qint32 main(int argc, char *argv[])
> > >>
> > >> {
> > >>
> > >> qint32 ret = 0;
> > >> QtSingleApplication shellApp(SHELL_APP_NAME, argc, argv);
> > >> Settings::Instance(&shellApp, SETTINGS_FILE_PREFIX);
> > >> LogFile::Instance(SHELL_LOG_FILE_PREFIX);
> > >> Debug("UPCShell::main()");
> > >>
> > >>
> > >> QString test = testQString("spot");
> > >> ret = shellApp.exec();
> > >> }
> > >>
> > >> Would you expect issues with the return type of the testQString
> > >> function.
> > >> We have many usages of this return type in our code and
> on Windows
> > >> its
> > >> seems to work fine, but on Mac OS X 10.5.1 using Xcode 3.0 (GCC
> > >> 4.0.1), we
> > >> have function like testQString which return garbage
> QStrings. Oddly
> > >> enough, this simple example works fine on the Mac.
> > >
> > > Did you try with some literal non-ascii characters? The
> QString(const
> > > char*) constructor uses fromAscii() in Qt4 (and fromLatin1 in Qt3,
> > > IIRC)
> > > to convert the char array into unicode and thus it will fail with
> > > anything but plain ascii. What you probably want is
> ::fromUtf8, though
> > > you have to make sure all your sources are utf8 encoded
> and any input
> > > from libraries that use const char* is utf8.
> > >
> > > Andreas
> > >
> > > --
> > > Communicate! It can't make things any worse.
> > >
> > > --
> > > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> > > with "unsubscribe" in the subject or the body.
> > > List archive and information:
> http://lists.trolltech.com/qt-interest/
> > >
> >
> > --
> > To unsubscribe - send a mail to
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
> subject or the body.
> > List archive and information:
> http://lists.trolltech.com/qt-interest/
> >
> >
>
> --
> To unsubscribe - send a mail to
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
> subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 6 in thread
tomcondon@xxxxxxx wrote:
> That is very good info for us, but what I was trying to get at was that
> the returned QString() creates a temporary QString. Is that going to be
> a sane value after the function returns. In some of our cases "return
> QString("spot")" is returning garbage, but only under GCC 4.0.1 on the
> Mac. Visual C++ is fine with it.
> On Dec 4, 2007, at 5:51 AM, Andreas Pakulat wrote:
>
>> On 04.12.07 05:32:49, tomcondon@xxxxxxx wrote:
>>> Given the code:
>>>
>>> QString testQString(char * input)
>>>
>>> {
>>> return QString(input);
>>> }
This should be fine. You are not returning a temporary
QString as the return value QString will ensure that
a new QString is constructed. OTOH, if you returned
QString &, you'd be in trouble, for the very reason
you pointed out. If this is failing, then it's not
due to an inherent problem with your code.
--
[ signature omitted ]