I have a destructor which checks if a pointer has been allocated, if it has, it deletes it:
ShadeRec::~ShadeRec(){
cout << "before deleting ShadeRec" << endl;
if(material){
cout << material << endl;
delete material;
material = NULL;
}
cout << "after deleting ShadeRec" << endl;
}
The first pointer goes through fine and then on the second one the program gives me the error.
I have checked with the cout
s and there is something inside the pointer, which makes sense as it got into the if statement... so why is it giving me the error?
Constructors:
ShadeRec::ShadeRec(World& world)
: hit(false),
material(NULL),
hitPoint(),
localHitPoint(),
normal(),
ray(),
depth(0),
colour(0),
t(0.0),
w(world)
{}
ShadeRec::ShadeRec(const ShadeRec& sr)
: hit(sr.hit),
material(sr.material),
hitPoint(sr.hitPoint),
localHitPoint(sr.localHitPoint),
normal(sr.normal),
ray(sr.ray),
depth(sr.depth),
colour(sr.colour),
t(sr.t),
w(sr.w)
{}
Material operator=
Material&
Material::operator= (const Material& rhs) {
if (this == &rhs)
return (*this);
return (*this);
}
Matte is a child of Material:
Matte* matte1 = new Matte;
which as both of these:
Matte& operator= (const Matte& rhs);
virtual Material* clone(void) const;