-2

I've a segfault when executing my code: according to GDB the segfault is thrown by

Program received signal SIGSEGV, Segmentation fault.
0x00000000004090a6 in std::vector<GeometricObject*, std::allocator<GeometricObject*> >::size (this=0x30)
at /usr/include/c++/4.9/bits/stl_vector.h:655
655       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }

and that's the output of GDB where command

#0  0x000000000040923c in std::vector<GeometricObject*, std::allocator<GeometricObject*> >::size (
this=0x30) at /usr/include/c++/4.9/bits/stl_vector.h:655
#1  0x000000000040843c in World::hitObjects (this=0x0, r=...)
at /mnt/7DEB96B84D6B013D/Universita/Programmazione ad oggetti e grafica/esercizi/Progetto - RTFGU/core/world/world.cpp:85
#2  0x000000000040a0d8 in MultipleObjects::traceRay (this=0x67ea70, r=...)
at /mnt/7DEB96B84D6B013D/Universita/Programmazione ad oggetti e grafica/esercizi/Progetto - RTFGU/core/tracers/multipleObjects.cpp:13
#3  0x00000000004086f0 in World::renderScene (this=0x7fffffffdd60)
at /mnt/7DEB96B84D6B013D/Universita/Programmazione ad oggetti e grafica/esercizi/Progetto - RTFGU/core/world/world.cpp:115
#4  0x0000000000407c05 in main (argc=1, argv=0x7fffffffdea8)
at /mnt/7DEB96B84D6B013D/Universita/Programmazione ad oggetti e grafica/esercizi/Progetto - RTFGU/main.cpp:20

Here is the main class

int main(int argc, char const *argv[]){

World w;

w.build();
w.renderScene();
w.displayImage();


return 0;
}

Here the World class with definitions

class World{
public:
 World();
~World();

void addObject(GeometricObject *);
void build();
ShadeRec hitObjects(const Ray &);
void renderScene() const;
void displayImage() const;
void displayPixel(const int, const int, const RGBColor &) const;

RGBColor backgroundColor;
ViewPlane vp;
Tracer * tracer_ptr;
cv::Mat * rendering;
std::vector<GeometricObject*> objects;
};

// definitions

World::World(): backgroundColor(),  vp(), tracer_ptr(0), rendering(0), objects(){}

World::~World(){}

void World::addObject(GeometricObject * o){
objects.push_back(o);
}


void World::build(){
std::cout << "Build begins" << std::endl;

int width = 1024, height = 768;

vp.setHres(width);
vp.setVres(height);
vp.setPixelSize(1.f);
vp.setGamma(1.f);

rendering = new cv::Mat(height,width, CV_8UC3, cv::Scalar(0,0,0));
tracer_ptr = new MultipleObjects(this);

backgroundColor = BLACK;

Sphere * s1 = new Sphere(Point(0., -25., 0.), 80., CYAN);
Sphere * s2 = new Sphere(Point(0.,30.,0.), 60., MAGENTA);
Plane * p = new Plane(Point(0.), Normal(0., 1., 1.), YELLOW);

std::cout << objects.size() << std::endl; // used for debug: size is 0


addObject(s1);
addObject(s2);
addObject(p);

std::cout << objects.size() << std::endl; // used for debug: size is 3

for(uint i = 0; i < objects.size(); i++) // in this scope objects.size() works
 std::cout << objects[i]->toString() << std::endl;

// at this stage gdb give me these information:
(gdb) p this
$1 = (World * const) 0x7fffffffdd60
(gdb) p *this
$2 = {backgroundColor = {r = 0, g = 0, b = 0}, vp = {hRes = 1024, vRes = 768, s = 1, gamma = 1, 
gammaInv = 1}, tracer_ptr = 0x67ea70, rendering = 0x67d870, 
objects = std::vector of length 3, capacity 4 = {0x67ea90, 0x67ead0,  0x67eb10}}
(gdb) p &objects
$4 = (std::vector<GeometricObject*, std::allocator<GeometricObject*> > *) 0x7fffffffdd90

}

ShadeRec World::hitObjects(const Ray & r){

// at this stage , instead
(gdb) p this
$1 = (World * const) 0x0
(gdb) p *this
Cannot access memory at address 0x0
(gdb) p objects
Cannot access memory at address 0x30
(gdb) p &objects
$2 = (std::vector<GeometricObject*, std::allocator<GeometricObject*> > *) 0x30


std::cout << "hitObjects begins" << std::endl;
ShadeRec sr(*this);
int objNum = objects.size(); // in this scope objects.size() throw a segfault
double t, tmin = viewLimit;

for(int i = 0; i < objNum ; i++){
 if(objects.at(i)->hit(r, t, sr) && t < tmin){
  sr.haveHit = true;
  sr.color = objects.at(i)->getColor();
  tmin = t;
 }
}
return sr;
}


void World::renderScene() const{
std::cout << "renderScene begins" << std::endl;

RGBColor col;
Ray ray;
double z = 10;
double x, y;

ray.d = Vect3(0.,0.,-1.);

for(int r = 0; r < vp.vRes; r++)
  for(int c = 0; c < vp.hRes; c++){
    x = vp.s*(c-(vp.hRes-1)*.5);
    y = vp.s*(r-(vp.vRes-1)*.5);
    ray.o = Point(x, y, z);
    std::cout << "debug - " << r << " " << c << std::endl;
    col = tracer_ptr->traceRay(ray);
    std::cout << "exit traceRay" << std::endl;
    rendering->at<cv::Vec3b>(cv::Point(c,r)) =    cv::Vec3b(col.b*255,col.g*255,col.r*255);
  }
}

void World::displayImage() const{

 cv::imwrite("rendering.png", *rendering);

 cv::namedWindow("Rendering", cv::WINDOW_AUTOSIZE );

 if(!rendering->empty())
  cv::imshow("Rendering", *rendering);

 cv::waitKey(0);
}

and at last the MultipleObject class that launch traceRay() in world::renderScene() and call world::hitObject() in itself

class MultipleObjects: public Tracer{
public:
  MultipleObjects();
  MultipleObjects(World *);
  ~MultipleObjects();

  RGBColor traceRay(const Ray &) const;

 protected:
  World * world_ptr;
};

// definitions

MultipleObjects::MultipleObjects(): Tracer(){}

MultipleObjects::MultipleObjects(World * w): Tracer(w){}

MultipleObjects::~MultipleObjects(){}

RGBColor MultipleObjects::traceRay(const Ray & r) const{
  ShadeRec sr(world_ptr->hitObjects(r));

  if(sr.haveHit)
    return sr.color;
  else
    return world_ptr->backgroundColor;
}

The question is: why in World::build() scope all works, whereas in World::hitObjects(), this point to NULL and &objects point to 0x30?

Thanks in advance for answers and sorry for my bad English and eventually for my first question.

ildjarn
  • 59,718
  • 8
  • 115
  • 201
Pepello
  • 11
  • 1
  • 3

2 Answers2

0

Looks like your world pointer is invalid.

If you allow a zombie state for your MultipleObjects class (aka no world pointer) least you can do is to initialize world_ptr to nullptr and do a check on that before trying to call anything on it.

Marco Giordano
  • 580
  • 3
  • 13
0

I've found that MultipleObjects class, that extends Tracer class, has a World * world_ptr variable too, in addition to that inherited by Tracer. So when I call:

tracer_ptr = new MultipleObjects(this);

Tracer's World* var is set to this , leaving MultipleObjects'S World* var empty.

Pepello
  • 11
  • 1
  • 3