| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 2 | |
Hi, all,
Please take a look at the following short program, which includes a
convert of QString to char*.
It is ok if I put toAscii() and data() into two lines, but
It crashes if I join the two lines into one.
What is matter of
qString.toAscii().data(); // ???????
Thanks,
Lingfa
//===================================================
void main()
{
FILE*fp=fopen("1.dat","w");
QString qString("Hello World!");
QByteArray tmp = qString.toAscii();
char *data1 = tmp.data();
fprintf(fp,"%s\n",data1); // ok !
// join two lines in one
char *data2 = qString.toAscii().data(); // ?
// fprintf(fp,"%s\n",data2); // crash ! why ?
fclose(fp);
}
//===================================================
--
[ signature omitted ]
lingfa wrote: > It is ok if I put toAscii() and data() into two lines, but > It crashes if I join the two lines into one. > What is matter of > > qString.toAscii().data(); // ??????? The function QString::toAscii() creates a new QByteArray which it returns and which you call ".data()" on. This QByteArray will then be destructed! After the destruction of the QByteArray the pointer to the data is not valid anymore. If you try to dereference it later, anything might happen. (Including a crash). If you keep the QByteArray, the pointer returned by data() will be valid (as long as you do not modify said byte array). Another solution would be to call: fprintf(fp,"%s\n",qString.toAscii().data()); since the QByteArray lives until after the pointer is needed (in this instance); Have a nice day - Jan Krämer -- [ signature omitted ]
Because data2 is a pointer to temporary data...
toAscii() returns a QByteArray, which is temporary in the second call,
but not in the first (since its on the stack)
Scott
> -----Original Message-----
> From: lingfa [mailto:lingfa@xxxxxxxxxxxx]
> Sent: Wednesday, January 10, 2007 8:48 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: join two lines in one, crash ! why?
>
> Hi, all,
>
> Please take a look at the following short program, which includes a
> convert of QString to char*.
> It is ok if I put toAscii() and data() into two lines, but
> It crashes if I join the two lines into one.
> What is matter of
>
> qString.toAscii().data(); // ???????
>
> Thanks,
> Lingfa
>
>
> //===================================================
> void main()
> {
> FILE*fp=fopen("1.dat","w");
>
> QString qString("Hello World!");
>
> QByteArray tmp = qString.toAscii();
> char *data1 = tmp.data();
> fprintf(fp,"%s\n",data1); // ok !
>
> // join two lines in one
> char *data2 = qString.toAscii().data(); // ?
> // fprintf(fp,"%s\n",data2); // crash ! why ?
>
> fclose(fp);
> }
> //===================================================
>
>
> --
> 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 ]
Your are right. I found data2 in debug mode was a junk.
Again,
fprintf(fp,"%s\n",qString.toAscii().data());
is ok, but
char *data2 = qString.toAscii().data();
fprintf(fp,"%s\n",data2);
is NOT ok. So how can a C-programmer accept this?
Lingfa
Scott Aron Bloom wrote:
>Because data2 is a pointer to temporary data...
>
>toAscii() returns a QByteArray, which is temporary in the second call,
>but not in the first (since its on the stack)
>
>Scott
>
>
>
>>-----Original Message-----
>>From: lingfa [mailto:lingfa@xxxxxxxxxxxx]
>>Sent: Wednesday, January 10, 2007 8:48 AM
>>To: qt-interest@xxxxxxxxxxxxx
>>Subject: join two lines in one, crash ! why?
>>
>>Hi, all,
>>
>>Please take a look at the following short program, which includes a
>>convert of QString to char*.
>>It is ok if I put toAscii() and data() into two lines, but
>>It crashes if I join the two lines into one.
>>What is matter of
>>
>> qString.toAscii().data(); // ???????
>>
>>Thanks,
>>Lingfa
>>
>>
>>//===================================================
>>void main()
>>{
>> FILE*fp=fopen("1.dat","w");
>>
>> QString qString("Hello World!");
>>
>> QByteArray tmp = qString.toAscii();
>> char *data1 = tmp.data();
>> fprintf(fp,"%s\n",data1); // ok !
>>
>> // join two lines in one
>> char *data2 = qString.toAscii().data(); // ?
>>// fprintf(fp,"%s\n",data2); // crash ! why ?
>>
>> fclose(fp);
>>}
>>//===================================================
>>
>>
>>--
>>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 ]
On 10.01.07 12:23:19, lingfa wrote: > Again, > fprintf(fp,"%s\n",qString.toAscii().data()); > is ok, but > char *data2 = qString.toAscii().data(); > fprintf(fp,"%s\n",data2); > is NOT ok. So how can a C-programmer accept this? This is Qt, a C++ Framework, so we don't care for C Programmers ;) Seriously, the issue is still that the QByteArray is destructed as soon as the semicolon is hit. During the deconstruction it will delete whatever its internal data pointer pointed to, so its the same thing as if you'd do: char* data2; fprintf(fp....); Got it now? You have to keep the QByteArray around. Andreas -- [ signature omitted ]
Fine as you said Qt doesn’t care C Programmers. Look at another crash: std::string stdString = qString.toStdString(); std::string s2 = stdString; // Crash! why? Does Qt care STL? Lingfa Andreas Pakulat wrote: >On 10.01.07 12:23:19, lingfa wrote: > > >>Again, >> fprintf(fp,"%s\n",qString.toAscii().data()); >>is ok, but >> char *data2 = qString.toAscii().data(); >> fprintf(fp,"%s\n",data2); >>is NOT ok. So how can a C-programmer accept this? >> >> > >This is Qt, a C++ Framework, so we don't care for C Programmers ;) > >Seriously, the issue is still that the QByteArray is destructed as soon >as the semicolon is hit. During the deconstruction it will delete >whatever its internal data pointer pointed to, so its the same thing as >if you'd do: > >char* data2; >fprintf(fp....); > >Got it now? You have to keep the QByteArray around. > >Andreas > > > -- [ signature omitted ]
On Wed, 10 Jan 2007 12:53:37 -0500 lingfa <lingfa@xxxxxxxxxxxx> wrote: > Fine as you said Qt doesnât care C Programmers. No, it's C++ that doesn't care C Programmers. -- [ signature omitted ]
On 10.01.07 12:53:37, lingfa wrote:
> Fine as you said Qt doesnât care C Programmers.
That was already answered and it seems to me you missed the ";)" at the
end of that line from me. It was meant as a joke.
> Look at another crash:
> std::string stdString = qString.toStdString();
> std::string s2 = stdString; // Crash! why?
> Does Qt care STL?
Hmm, that works for me perfectly, see:
,----
| #include <QString>
| #include <iostream>
| int main(int argc, char** argv)
| {
| QString s("FOOBAR");
| std::string s1 = s.toStdString();
| std::string s2 = s1;
| std::cout << s2 << std::endl;
| }
`----
So my only guess is that you didn't show us the full example. Also
providing a backtrace for the crash would help identifying the problem.
Andreas
--
[ signature omitted ]
I get your joke. Problem is I copied your "FOOBAR", it also crashed.
I am using Qt4.1.1 on a windows machine. Does the compiler setting have
some tricks?
Lingfa
Andreas Pakulat wrote:
>On 10.01.07 12:53:37, lingfa wrote:
>
>
>>Fine as you said Qt doesnât care C Programmers.
>>
>>
>
>That was already answered and it seems to me you missed the ";)" at the
>end of that line from me. It was meant as a joke.
>
>
>
>>Look at another crash:
>>std::string stdString = qString.toStdString();
>>std::string s2 = stdString; // Crash! why?
>>Does Qt care STL?
>>
>>
>
>Hmm, that works for me perfectly, see:
>
>,----
>| #include <QString>
>| #include <iostream>
>| int main(int argc, char** argv)
>| {
>| QString s("FOOBAR");
>| std::string s1 = s.toStdString();
>| std::string s2 = s1;
>| std::cout << s2 << std::endl;
>| }
>`----
>
>So my only guess is that you didn't show us the full example. Also
>providing a backtrace for the crash would help identifying the problem.
>
>Andreas
>
>
>
--
[ signature omitted ]
Hi,
for me, it works fine on a windows XP Pro with Qt 4.2.2. I'm using the
following pro-file:
SOURCES += test.cpp
CONFIG -= debug_and_release
CONFIG += console
Ralf
lingfa schrieb:
>
> I get your joke. Problem is I copied your "FOOBAR", it also crashed.
> I am using Qt4.1.1 on a windows machine. Does the compiler setting have
> some tricks?
>
> Lingfa
>
>
> Andreas Pakulat wrote:
>
>> On 10.01.07 12:53:37, lingfa wrote:
>>
>>
>>> Fine as you said Qt doesnât care C Programmers.
>>>
>>
>> That was already answered and it seems to me you missed the ";)" at the
>> end of that line from me. It was meant as a joke.
>>
>>
>>
>>> Look at another crash:
>>> std::string stdString = qString.toStdString();
>>> std::string s2 = stdString; // Crash! why?
>>> Does Qt care STL?
>>>
>>
>> Hmm, that works for me perfectly, see:
>>
>> ,----
>> | #include <QString>
>> | #include <iostream>
>> | int main(int argc, char** argv)
>> | {
>> | QString s("FOOBAR");
>> | std::string s1 = s.toStdString();
>> | std::string s2 = s1;
>> | std::cout << s2 << std::endl;
>> | }
>> `----
>>
>> So my only guess is that you didn't show us the full example. Also
>> providing a backtrace for the crash would help identifying the problem.
>>
>> Andreas
>>
--
[ signature omitted ]
This is not a C issue, this is not understanding variable lifespan issue. All variables on the stack eventually lose scope and the destructor for that object is called. You need to undertstand what the scope is for temporary variables in c++, without this basic understanding, your work towards QT will be much more difficult. Scott > Fine as you said Qt doesn't care C Programmers. > Look at another crash: > std::string stdString = qString.toStdString(); > std::string s2 = stdString; // Crash! why? > Does Qt care STL? > > Lingfa > > > Andreas Pakulat wrote: > > >On 10.01.07 12:23:19, lingfa wrote: > > > > > >>Again, > >> fprintf(fp,"%s\n",qString.toAscii().data()); > >>is ok, but > >> char *data2 = qString.toAscii().data(); > >> fprintf(fp,"%s\n",data2); > >>is NOT ok. So how can a C-programmer accept this? > >> > >> > > > >This is Qt, a C++ Framework, so we don't care for C Programmers ;) > > > >Seriously, the issue is still that the QByteArray is destructed as soon > >as the semicolon is hit. During the deconstruction it will delete > >whatever its internal data pointer pointed to, so its the same thing as > >if you'd do: > > > >char* data2; > >fprintf(fp....); > > > >Got it now? You have to keep the QByteArray around. > > > >Andreas > > > > > > > > > -- > 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 ]
Am Mittwoch, 10. Januar 2007 20:04 schrieb Scott Aron Bloom: > This is not a C issue, this is not understanding variable lifespan > issue. I think this wouldn't be a problem, if std::basic_string would not seem to keep its data() pointers valid, since it's based on a character buffer and just gives a reference to it. *sigh* lg Erik -- [ signature omitted ]
Attachment:
pgpbxyMAWtOYC.pgp
Description: PGP signature
No.. its not std::basic_string, its QByteArray, I was never able to reproduce his issue in STL... data() from QByteArray does not increment the ref count on QByteArray, and it shouldn't. The temporary QByteArray should destruct after the statement, its temporary.... Scott > -----Original Message----- > From: erik.meusel@xxxxxx [mailto:erik.meusel@xxxxxx] > Sent: Wednesday, January 10, 2007 12:17 PM > To: qt-interest@xxxxxxxxxxxxx > Subject: Re: join two lines in one, crash ! why? > > Am Mittwoch, 10. Januar 2007 20:04 schrieb Scott Aron Bloom: > > This is not a C issue, this is not understanding variable lifespan > > issue. > I think this wouldn't be a problem, if std::basic_string would not seem to > keep its data() pointers valid, since it's based on a character buffer and > just gives a reference to it. *sigh* > > lg > Erik > > -- > BOFH excuse #150: > > Arcserve crashed the server again. -- [ signature omitted ]
BTW, QT works fine with STL... Make sure your STL settings are set
properly for both the QT build lib (assuming a share library for QT) and
for the app. My example
int main( int argc, char ** argv)
{
QString qString( "Hello" );
std::string stdString = qString.toStdString(); std::string s2 =
stdString;
}
Works fine for me.
Scott
> Fine as you said Qt doesn't care C Programmers.
> Look at another crash:
> std::string stdString = qString.toStdString();
> std::string s2 = stdString; // Crash! why?
> Does Qt care STL?
>
> Lingfa
>
>
> Andreas Pakulat wrote:
>
> >On 10.01.07 12:23:19, lingfa wrote:
> >
> >
> >>Again,
> >> fprintf(fp,"%s\n",qString.toAscii().data());
> >>is ok, but
> >> char *data2 = qString.toAscii().data();
> >> fprintf(fp,"%s\n",data2);
> >>is NOT ok. So how can a C-programmer accept this?
> >>
> >>
> >
> >This is Qt, a C++ Framework, so we don't care for C Programmers ;)
> >
> >Seriously, the issue is still that the QByteArray is destructed as
soon
> >as the semicolon is hit. During the deconstruction it will delete
> >whatever its internal data pointer pointed to, so its the same thing
as
> >if you'd do:
> >
> >char* data2;
> >fprintf(fp....);
> >
> >Got it now? You have to keep the QByteArray around.
> >
> >Andreas
> >
> >
> >
>
>
> --
> 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 ]
Mine doesn't work. I tracked in, and saw the call to:
inline std::string QString::toStdString() const
{ const QByteArray asc = toAscii(); return
std::string(asc.constData(), asc.length()); }
Then, it crashed at s2 assignment.
However, I made a copy above and directly pasted in, all fine! Crazy !
Lingfa
#include <QString.h>
#include <string>
void main()
{
FILE*fp=fopen("1.dat","w");
QString qString("Hello World!");
QByteArray tmp = qString.toAscii();
char *data1 = tmp.data();
fprintf(fp,"%s\n",data1); // ok !
// Crash 1:
char *data2 = qString.toAscii().data(); // to garbage!
// fprintf(fp,"%s\n",data2); // crash !
fprintf(fp,"%s\n",qString.toAscii().data()); // ok !
// Crash 2:
// std::string stdString = qString.toStdString(); // ?
// std::string s2 = stdString; // Crash! why?
// walk around. All fine!
const QByteArray asc = qString.toAscii();
std::string stdString = std::string(asc.constData(), asc.length());
std::string s3 = stdString;
fclose(fp);
}
Lingfa
Scott Aron Bloom wrote:
>BTW, QT works fine with STL... Make sure your STL settings are set
>properly for both the QT build lib (assuming a share library for QT) and
>for the app. My example
>
>int main( int argc, char ** argv)
>{
> QString qString( "Hello" );
> std::string stdString = qString.toStdString(); std::string s2 =
>stdString;
>}
>
>Works fine for me.
>
>Scott
>
>
>
>>Fine as you said Qt doesn't care C Programmers.
>>Look at another crash:
>>std::string stdString = qString.toStdString();
>>std::string s2 = stdString; // Crash! why?
>>Does Qt care STL?
>>
>>Lingfa
>>
>>
>>Andreas Pakulat wrote:
>>
>>
>>
>>>On 10.01.07 12:23:19, lingfa wrote:
>>>
>>>
>>>
>>>
>>>>Again,
>>>> fprintf(fp,"%s\n",qString.toAscii().data());
>>>>is ok, but
>>>> char *data2 = qString.toAscii().data();
>>>> fprintf(fp,"%s\n",data2);
>>>>is NOT ok. So how can a C-programmer accept this?
>>>>
>>>>
>>>>
>>>>
>>>This is Qt, a C++ Framework, so we don't care for C Programmers ;)
>>>
>>>Seriously, the issue is still that the QByteArray is destructed as
>>>
>>>
>soon
>
>
>>>as the semicolon is hit. During the deconstruction it will delete
>>>whatever its internal data pointer pointed to, so its the same thing
>>>
>>>
>as
>
>
>>>if you'd do:
>>>
>>>char* data2;
>>>fprintf(fp....);
>>>
>>>Got it now? You have to keep the QByteArray around.
>>>
>>>Andreas
>>>
>>>
>>>
>>>
>>>
>>--
>>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 ]