jeudi 25 mai 2017

Why const QString& param return bad const char* pointer to data

Find out interesting bug: when I pass const QString& parameter and then get const char* pointer to data that stored within const QString&, pointer points to bad data (some characters that not seemed as I send to function).

LogbookNote LBHasher::makePropsFromPseudoElement(const QString& id)
{
   LogbookNote rv;
   if (m_ctx->currentTrim) {
    const char* charId = id.toLatin1().data();
    ...
    qDebug() << charId;
}

And calling code:

LogbookNote LBHasher::makePropsByNameConvention(QString id)
{
   LogbookNote g;
   if (id.startsWith("_ctx_")) {
       g = makePropsFromPseudoElement(id);
 ...
 }

As you can mention, to calling method id comes as QString, i.e. whole copy of QString object. And thus const QString& can be a strong (not broken, to deleted temporary object, for example) reference.

Why such behavior occurred?

Aucun commentaire:

Enregistrer un commentaire