Storing the result of
char *dangling_pointer = strName.toLocal8Bit().data()
char *dangling_pointer = strName.toLocal8Bit().data()
To copy to clipboard, switch view to plain text mode
and variants is dangerous because data() returns a pointer to a temporary QByteArray's data. That QByteArray goes out of scope at the end of the assignment statement leaving a dangling pointer. If you cannot use the intermediate value immediately then best to make the QByteArray explicit. Either:
res = somefunc(strName.toLocal8Bit().data());
res = somefunc(strName.toLocal8Bit().data());
To copy to clipboard, switch view to plain text mode
or
// intervening stuff
res = somefunc(ba.data());
QByteArray ba = strName.toLocal8Bit();
// intervening stuff
res = somefunc(ba.data());
To copy to clipboard, switch view to plain text mode
should be safe (but I am fairly new to the C++ game and happy to be corrected).
Bookmarks