What does this error in Valgrind mean? I've looked at a lot of other posts but I still don't understand what the error actually means. Is there a problem with my Dragon destructor function?
==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)
EDIT: This is what my Dragon class looks like:
#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
EDIT: Basically my code does this:
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;
}