Valgrind에서이 오류는 무엇을 의미합니까? 다른 게시물을 많이 보았지만 실제로 오류의 의미를 이해하지 못합니다. 드래곤 소멸자 함수에 문제가 있습니까?Valgrind의 크기가 8입니다. C++
==48500== Invalid read of size 8
==48500== at 0x40AF66: Dragon::~Dragon() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x40AFDB: Dragon::~Dragon() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x406841: Floor::deleteAll() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x4022D8: Floor::~Floor() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x401EE1: main (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== Address 0x5a1d130 is 0 bytes inside a block of size 40 free'd
==48500== at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==48500== by 0x40A7B3: Treasure::~Treasure() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x40671B: Floor::deleteAll() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x4022D8: Floor::~Floor() (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
==48500== by 0x401EE1: main (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
편집 : 이 내 드래곤 클래스는 모습입니다 같은 :
#include "dragon.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
Dragon::Dragon(Item *hoard) {
// initalizes some variables
treasureHoard = hoard;
}
Dragon::~Dragon(){
treasureHoard->deadDragon();
}
// some functions
편집 :
D *ptr=new D();
// some code …
if (ptr->isDead()) {
delete ptr;
ptr = NULL;
}
// some code
// at the end of the program:
if (ptr) { // <<< is this causing the error?
delete ptr;
ptr = NULL;
}
여기 (간단한 스택 있지만) 문제를 복제하는 완벽한 프로그램입니다. 초기화되지 않은 메모리를 읽는 중입니다. – erenon
링크 http://stackoverflow.com/a/22658693/2724703 –