
Originally Posted by
DPinLV
I don't see why the byte array should be deleted here since it (or it's pointer) is still in scope, no?
The pointer is in scope, but the temporary QByteArray isn't. QString::toLatin1() returns a completely new QByteArray object --- not some reference to an internal buffer. On the other hand QByteArray::data() returns a pointer to internal data of that QByteArray, so if it gets destroyed, that pointer will be invalid.

Originally Posted by
DPinLV
The QByteArray help says, "QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects", so shouldn't it be available to me to display?
It also says:
The pointer remains valid as long as the byte array isn't reallocated.
You should use QString::toLatin1() this way:
char *cstr = ba.data(); // you can use cstr as long as ba exist
QString str( "some string" );
QByteArray ba( str.toLatin1() );
char *cstr = ba.data(); // you can use cstr as long as ba exist
To copy to clipboard, switch view to plain text mode
but you do it like this:
char *cstr = str.toLatin1().data(); // wrong
char *cstr = str.toLatin1().data(); // wrong
To copy to clipboard, switch view to plain text mode
this uses a temporary QByteArray that ceases to exist immediately, so the workaround is to copy that string:
char *cstr = qstrdup( str.toLatin1().data() ); // ok, but remember to delete cstr
char *cstr = qstrdup( str.toLatin1().data() ); // ok, but remember to delete cstr
To copy to clipboard, switch view to plain text mode
Bookmarks