안녕하세요이 코드내 트리플렛 객체가 왜 파괴됩니까?
class Triplets
{
public:
int nVal1;
int nVal2;
NodeT *ptrNode;
bool bUpdate;
Triplets()
{
cout << "creating empty triplet" << endl;
nVal2 = 0;
nVal1 = 0;
bUpdate = false;
ptrNode = NULL;
}
~Triplets()
{
cout << "destroying triplet" << endl;
delete ptrNode;
}
Triplets(int nVal1, int nVal2, bool bUpdate, NodeT *ptrNode)
{
cout << "creating triple with values" << endl;
this->nVal2 = nVal2;
this->nVal1 = nVal1;
this->bUpdate = bUpdate;
this->ptrNode = ptrNode;
}
};
이 사용
void find_triplets(NodeT *ptrRoot)
{
if (ptrRoot != NULL)
{
find_triplets(ptrRoot->left);
find_triplets(ptrRoot->right);
cout << "find triplets and save them to the queue" << endl;
cout << " we hit a hot spot is null the root, nothing to see here move along boys" << endl;
if(ptrRoot->left != NULL && ptrRoot->right != NULL)
{
if (ptrRoot->left->done == true && ptrRoot->right->done == true)
{
cout << "we got one of 2 sons true so do something, this are the sons "<< ptrRoot->left->key_value << " " << ptrRoot->right->key_value << endl;
cout << "sum them and put it in the father and set it to true " << endl;
ptrRoot->key_value = ptrRoot->left->key_value + ptrRoot->right->key_value;
ptrRoot->done = true;
cout << "thread queue " << endl;
Triplets triplet(ptrRoot->left->key_value, ptrRoot->right->key_value, ptrRoot->done, ptrRoot);
qThreads.push(triplet);
}
}
}
}
그리고이 주요
MyThread mt;
mt.start();
mt.run();
cout << "sum = " << mt.sum(&bt) << endl;
THREADS_HOW_MANY = boost::thread::hardware_concurrency();
std::cout << THREADS_HOW_MANY << std::endl;
while (!bt.Values_to_do.empty())
{
cout << "da vals to sum are these " << bt.Values_to_do.front() << " and aready done " << bt.values_done.front() << endl;
bt.Values_to_do.pop();
bt.values_done.pop();
}
while(!mt.qThreads.empty())
{
cout << "da triplet are " << mt.qThreads.front().nVal1 << " " <<mt.qThreads.front().nVal2 << " "
<< mt.qThreads.front().ptrNode << " " << mt.qThreads.front().bUpdate
<< endl;
mt.qThreads.pop();
}
이 모든 것은 나에게 (명백하게 잘못된)이 결과
done lets chek
find triplets
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
we got one of 2 sons true so do something, this are the sons 2 8
sum them and put it in the father and set it to true
thread queue
creating triple with values
destroying triplet
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
we got one of 2 sons true so do something, this are the sons 11 15
sum them and put it in the father and set it to true
thread queue
creating triple with values
destroying triplet
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
we got one of 2 sons true so do something, this are the sons 19976608 19976464
sum them and put it in
the father and set it to true
thread queue
creating triple with values
destroying triplet
the gran total is 19976320
을 줄이
이제 제 q가 제 큐브에 추가되지 않고 그 대신에 파괴되는 트리플렛 오브젝트입니다. 그것이 합계가 엉망이되어 버리는 원인입니다. 감사.
코드를보고 나에게 무엇을 달성하고자하는지 명확하지 않으므로 변경해야 할 것이 명확하지 않습니다. 그러나 일반적인 메모리 관리, 트리를 꺼내지 않고 노드를 삭제하고 왼쪽과 오른쪽 모두가 null이 아닌 경우에만 동작하는 것은 나에게 문제가되는 것처럼 보입니다. – stefaanv
나는 트리 클래스가 그것을 처리한다는 것을하지 않는다. 여기에 오랫동안 배치하는 것은이 클래스는 숫자가있는 두 아들과 합계를 저장하는 아버지에 대한 포인터가있는 트리플렛이다. 그래서 내가하고 싶은 것은 실제 객체를 대기열에 저장하여 원하는대로 수정할 수 있도록하는 것입니다. –
그러나 트리플릿 소멸자에서 노드가 삭제되고 노드에 대한 포인터로 새 트리플릿을 만든 다음 큐에서 복사본을 밀어 스코프를 벗어날 때 새 트리플릿이 삭제되도록합니다. 그러면 노드가 삭제되지 않고 노드가 삭제됩니다 나무에서. – stefaanv