mardi 16 octobre 2018

Return initializer list from local variable by value

I'm getting free(): invalid size when trying to do something like below:

std::u32string FileMappingWrapper::getCurrCharPosAsString() const
{
    std::u32string progressString;
    size_t currLine = getFrameStartPos();
    while (currLine)
    {
        progressString.push_back(U'0' + currLine % 10);
        currLine /= 10;
    }
    return {progressString.rbegin(), progressString.rend()}; <- I expect that std::u32string rvalue constructor or RVO will be used
}

Then I call this functions from other one:

void CurrFrameVisitor::visit(windows::NCursesProgressWindowDecorator *window)
{
    RawWindowBuffer newBuffer(window->getWindowDimensions().first, window->getWindowDimensions().second);
    auto progress = window->getParentWindowHandler()->getFileMapper()->getPrintedProgress();
    auto lineNo = window->getParentWindowHandler()->getFileMapper()->getCurrCharPosAsString();
    std::copy(progress.begin(), progress.end(), newBuffer[0].begin());
    newBuffer[0][progress.size()] = '%';
    if (lineNo.empty()) {
        lineNo.push_back(U'0');
    }
    std::copy(lineNo.begin(), lineNo.end(), std::next(newBuffer[0].begin(), progress.size() + 2));
    window->print(std::move(newBuffer)); <- All is good untill this point
}

I know the example cannot be compiled, but here is the thing I debugged the code and I can see that when calling visitor all is good, until stack unwinding. Trying to destroy variable created from initializer list auto lineNo = window->getParentWindowHandler()->getFileMapper()->getCurrCharPosAsString(); raises SIGABRT. So my question is: Is retuning initializer list by value from local object like I did safe and valid? Thanks

Aucun commentaire:

Enregistrer un commentaire