简体   繁体   English

在C ++ 11中使用async进行分段错误(核心转储)

[英]Segmentation fault (core dumped) using async in C++11

I created a function to convert an existing tree object to a string. 我创建了一个将现有树对象转换为字符串的函数。 Format of the string is 字符串的格式是

parent ( child1 ) ( child2 ( childOfChild2 ) )

The program outputs the string correctly, does some other work, but at the and crashes with 程序正确输出字符串,做一些其他的工作,但在和崩溃

Segmentation fault (core dumped)

This is the function ( getTree(this->root) outputs whole tree): 这是函数( getTree(this->root)输出整个树):

template <typename T>
string Tree<T>::getTree(const Node<T>& node) {
    if (node.isLeaf()){
        return to_string(node.value);
    }

    vector<future<string>> results; //each element represents a subtree connected to "node"
    for (auto child:node.children){
        results.push_back(move(async(&Tree<T>::getTree,this,ref(*child))));
    }

    string children("");
    for (auto& result:results){
        children+=string(" (") + result.get()+ string(") ");     //this creates Segmentation fault, but the output is correct
        //children+=string("");                                  //this does not create Segmentation fault
        //children+=string("foo");                               //this creates Segmentation fault
    }

    return to_string(node.value)+children;
}  

Some info about the variables: 有关变量的一些信息:

vector<shared_ptr<Node>> children;

Please tell if you need more info. 请告诉您是否需要更多信息。 The full source is tree.cpp and tree.h . 完整的源代码是tree.cpptree.h。

Your comparison function in the iterator doesn't work - you also need to cover the case where both containers are empty, ie 迭代器中的比较函数不起作用 - 您还需要覆盖两个容器都是空的情况,即

bool operator!= (const Iter& other) const {
    if (this->queueToIter.empty()&& other.queueToIter.empty()) return false;
    if (this->queueToIter.empty()&&! other.queueToIter.empty()) return true;
    if (!this->queueToIter.empty()&& other.queueToIter.empty()) return true;
    return (this->queueToIter.front())!=(other.queueToIter.front());
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM