[Qt-interest] Strange (as for me) QByteArray behavior
Mandeep Sandhu
mandeepsandhu.chd at gmail.com
Wed Dec 8 07:36:30 CET 2010
> --- 2 ---
> QString username = config.username; // config.username is a QString
> QByteArray tmp = username.toAscii();
> char *ptr = tmp.data();
> pjsua_acc_config acc;
> acc.cred_info[0].username = pj_str(ptr);
> --- 2 ---
>
> and this is working properly.
>
> So my question is what is the difference between solution 1 and 2? I
I too have burnt my fingers with this problem (i used to get weird
results each time I used to access the returned char patr)! :)
The problem here is due to the automatic variable used in 1. The
QByteArray returned as part of the toAscii() call (on which we are
calling .data()) is an automatic variable whose scope is only that
particular line of code. So after this stmt
(username.toAscii().data()) the QbyteArray is no longer valid and so
is the pointer returned by data().
The QByteArray returned from toAscii() must be in scope for the
pointer returned by data() to be valid.
CMIIW.
HTH,
-mandeep
> didn't noticed the difference of strings for example in qDebug() << ptr
> and qDebug() << username.toAscii().data(), so I'm really confused.
>
> Regards,
> Marek Bronowicki
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list