Segmentation error (kernel flushing) using async in C ++ 11

I created a function to convert an existing tree object to a string. Line format:

parent ( child1 ) ( child2 ( childOfChild2 ) ) 

The program displays the line correctly, performs some other work, but at the same time with errors

 Segmentation fault (core dumped) 

This function ( getTree(this->root) displays whole trees):

 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; } 

Variable Information:

 vector<shared_ptr<Node>> children; 

Tell me if you need more information. The full source is tree.cpp and tree.h.

+4
source share
1 answer

Your comparison function in the iterator does not work - you also need to close the case when both containers are empty, i.e.

 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()); }; 
+1
source

Source: https://habr.com/ru/post/1442212/


All Articles