How to prevent QStringBuilder from outliving the scope it was initialised in
Asked Answered
F

2

2

I have been looking at changing some code to make use of QStringBuilder expression template for its purported performance improvements. Unfortunately this resulted in sections of my code starting to crash in places, an example of which follows:

#define QT_USE_QSTRINGBUILDER
#include <numeric>
#include <vector>
#include <QString>
#include <QStringBuilder>

int main()
{
    std::vector<int> vals = {0, 1, 2, 3, 4};
    QString text = "Values: " + QString::number(vals[0]);
    text = std::accumulate(vals.begin() + 1, vals.end(), text, [](const QString& s, int i)
    {
        return s + ", " + QString::number(i);
    });
}

The reason this crashes is because the return type of the lambda expression is deduced to be QStringBuilder<QStringBuilder<QString,const char [3]>,QString> which after being returned attempts to cast to QString to assign to the accumulate result. This cast crashes because it is attempting to use references to objects which were in the scope of the lambda and have now been destroyed. This crash can be fixed by explicitly specifying the return type of the lambda as such [](const QString& s, int i) -> QString which ensures that the cast occurs before the closure is exited.

However the fact that enabling QStringBuilder here caused a crash in previously working code without even emitting a warning means that I will now avoid using it elsewhere unless I can guarantee that this will not happen again. Since in this case RVO could prevent any copying from occurring I don't think the usual technique of disabling copying of the object would work. Is there a way to prevent this occurring in QStringBuilder or similar expression templates or are objects which maintain references to automatic variables always going to be unsafe to use?

Festinate answered 9/9, 2015 at 10:5 Comment(4)
It's a Qt bug. If not already filed, please file it, and it should get fixed quickly.Intermeddle
QStringBuilder is C++11-compatible, but not C++14-compatible at the moment...This is essentially a C++14, not C++11 question.Intermeddle
@KubaOber Return types of lambda expressions with a single return statement need not be specified in C++11 which is what causes this issue.Festinate
@KubaOber This bug has already been filed and closed as 'out-of-scope'.Festinate
A
0

The lambda should return an explicit type:

[]() -> Type { }

or 

[]() -> QString { }
Arsenide answered 9/9, 2015 at 14:59 Comment(2)
I mention in the question that this fixes the problem for this instance but I am not seeking to fix this particular crash but rather the QStringBuilder class itself since return type specification is not mandatory for lambdas and not even for functions in C++14. The risk of causing a crash through this implicit behaviour is currently unacceptable.Festinate
Uh, I should have read the question thoroughly or maybe that was added later.Arsenide
S
0

The clazy code checker provides a check for this scenario.

Semitrailer answered 17/11, 2017 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.