2013-08-27 2 views
0

내 코드는 두 개의 클래스가 포함되어 있습니다.*** glibc는 감지 *** ./parent_child_conscall : 더블 무료 또는 손상 (fasttop) : 0x09d9e028는 ***

class points{ 
      char *p; 
    public: points(){cout<<"constructor points called"<<endl;p=new char();} 
      virtual ~points(){cout<<"destructor points called"<<endl;delete(p);} 
    }; 
    class otherpoints: public points{ 
        points x1; 
    public: otherpoints(){cout<<"constructor otherpoints called"<<endl;x1=points();} 
      ~otherpoints(){cout<<"destructor otherpoints called"<<endl;} 
    }; 
    int main(int argc, char *argv[]) 
    { 
    otherpoints y1; 

    return 0; 
    } 

여기에서는 기본 클래스 생성자에 포인터를 할당하고 해당 기본 클래스 소멸자의 포인터 메모리를 파괴합니다. 내가 바이너리를 사용 Valgrind의를 실행하면

, 그것은 오류가 있습니다 : -

constructor points called 

constructor points called 

constructor otherpoints called 

constructor points called 

destructor points called 

destructor otherpoints called 

destructor points called 

==2209== Invalid free()/delete/delete[] 

==2209== at 0x4024851: operator delete(void*) (vg_replace_malloc.c:387) 

==2209== by 0x8048A36: points::~points() (in home/santosh/programs/c++/parent_child_conscall) 

==2209== by 0x8048BB2: otherpoints::~otherpoints() (in /home/santosh/programs/c++/parent_child_conscall) 

==2209== by 0x804889A: main (in /home/santosh/programs/c++/parent_child_conscall) 

==2209== Address 0x42d5098 is 0 bytes inside a block of size 1 free'd 

==2209== at 0x4024851: operator delete(void*) (vg_replace_malloc.c:387) 

==2209== by 0x8048A36: points::~points() (in /home/santosh/programs/c++/parent_child_conscall) 

==2209== by 0x8048B32: otherpoints::otherpoints() (in /home/santosh/programs/c++/parent_child_conscall) 

==2209== by 0x8048889: main (in /home/santosh/programs/c++/parent_child_conscall) 

==2209== destructor points called 

==2209== 

==2209== HEAP SUMMARY: 

==2209==  in use at exit: 1 bytes in 1 blocks 

==2209== total heap usage: 3 allocs, 3 frees, 3 bytes allocated 

==2209== 

==2209== LEAK SUMMARY: 

==2209== definitely lost: 1 bytes in 1 blocks 

==2209== indirectly lost: 0 bytes in 0 blocks 

==2209==  possibly lost: 0 bytes in 0 blocks 

==2209== still reachable: 0 bytes in 0 blocks 

==2209==   suppressed: 0 bytes in 0 blocks 

==2209== Rerun with --leak-check=full to see details of leaked memory 

==2209== 

==2209== For counts of detected and suppressed errors, rerun with: -v 

==2209== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 7) 

내가 할당을 취소 할 수없는 생각하는 1 바이트 메모리 알 수 없습니다

.

내가 역 추적 보고서를 게시해야합니까?

어떤 도움 감사합니다.

+1

읽기 (http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29). –

+0

당신은'points'에게 할당 연산자와 복사 생성자를 줄 필요가 있습니다. * 3의 법칙을 찾는다. – juanchopanza

답변

3

문제는 여기에 당신이 points 클래스의 두 인스턴스를 만들 수 있다는 것입니다. 하나는 임시로 points()이고, 물론 하나는 x1입니다. 당신이 x1 = points()을 수행 할 때 당신이 points() 구조와 임시 객체를 생성

,이 후 x1에 복사 할당 한 다음 임시 객체를 파괴한다. 복사 할당 연산자를 제공하지 않으므로 컴파일러에서 자동으로 연산자를 만들지 만 단순히 포인터를 복사하고 새 메모리를 할당하지는 않습니다. 즉, 한 동안 동일한 메모리에 대한 포인터를 포함하는 두 개의 객체가 있음을 의미합니다. 임시 개체가 소멸 될 때 소멸자에서 메모리를 해제하고 x1에 포인터를 매달아 놓습니다. x1이 파괴되면이 매달려있는 포인터를 삭제하려고 시도하여 이중 자유 오류가 발생합니다.

이 문제를 해결하기위한 가장 좋은 방법은 복사 할당 연산자 구현이고, 바람직하게는 복사 생성자뿐만 아니라, 그 데이터를 새로운 메모리를 할당하고 복사한다. [세 가지의 규칙]에 대한

관련 문제