2012-06-19 3 views
0

안녕하세요이 코드내 트리플렛 객체가 왜 파괴됩니까?

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가 제 큐브에 추가되지 않고 그 대신에 파괴되는 트리플렛 오브젝트입니다. 그것이 합계가 엉망이되어 버리는 원인입니다. 감사.

+0

코드를보고 나에게 무엇을 달성하고자하는지 명확하지 않으므로 변경해야 할 것이 명확하지 않습니다. 그러나 일반적인 메모리 관리, 트리를 꺼내지 않고 노드를 삭제하고 왼쪽과 오른쪽 모두가 null이 아닌 경우에만 동작하는 것은 나에게 문제가되는 것처럼 보입니다. – stefaanv

+0

나는 트리 클래스가 그것을 처리한다는 것을하지 않는다. 여기에 오랫동안 배치하는 것은이 클래스는 숫자가있는 두 아들과 합계를 저장하는 아버지에 대한 포인터가있는 트리플렛이다. 그래서 내가하고 싶은 것은 실제 객체를 대기열에 저장하여 원하는대로 수정할 수 있도록하는 것입니다. –

+0

그러나 트리플릿 소멸자에서 노드가 삭제되고 노드에 대한 포인터로 새 트리플릿을 만든 다음 큐에서 복사본을 밀어 스코프를 벗어날 때 새 트리플릿이 삭제되도록합니다. 그러면 노드가 삭제되지 않고 노드가 삭제됩니다 나무에서. – stefaanv

답변

0

T1 내지; 박사 - 무엇을 팝업하는 것은 당신이 메모리 관리 소멸자 가지고있다 :

~Triplets() 
{ 
    cout << "destroying triplet" << endl; 
    delete ptrNode; 
} 

아직 복사 생성자와 대입 연산자를. 그것들을 구현해보십시오.

+0

참으로 나는 포인터를 돌보아야하는데, 왜 그 포인터가 필요한가요? –

+0

@ user1423656 세 규칙을 따르십시오. –

+0

@ user1423656 http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) –

0

당신은 Triplets 클래스의 복사본을 만들 것 : 당신이 복사 된 qThreads에 밀어 넣습니다 때 qThreads의 선언에 따라

Triplets triplet(ptrRoot->left->key_value, ptrRoot->right->key_value, ptrRoot->done, ptrRoot); 
qThreads.push(triplet); 

, 당신은 먼저, triplet 이름 Triplets의 인스턴스를 만듭니다. 그런 다음 범위가 다음 ( })에 있으면 triplet 인스턴스가 삭제됩니다. 이것은 물론 delete 안에있는 포인터와 사본에있는 포인터가 유효하지 않음을 의미합니다.

+0

참으로 나는 여러 객체를 만들고 큐에 추가하려고한다. qThreads는이 큐이다. qThreads, 나는 복사본을 원하지 않는다. 대기열에있는 객체가 복사본이 아니길 원한다는 것을 말해야합니까? –

+0

@ user1423656 _copy_를 대기열에두고 _original_이 파괴되었습니다. 포인터로 무엇을 할 것인지 생각하십시오. –

+0

그래서 큐에 들어가서 어떤 복사본이 아닌 개체를 만들고, 새로운 개체 트리플을 만들고이를 큐에 추가하십시오. –

관련 문제