inheritance and polymorphism issue

Hello forum,

I believe that i have the design problem that i need to discuss about.

Intersectable is the super-class; Triangle and Sphere are the sub-classes of the Intersectable

i believe that you already realized class inheritance structure. I have the following pure virtual functions declared inside the INTERSECTABLE

1
2
3
4
   virtual bool sampleDirection(const Intersection&,Vector3D&) = 0;

   virtual float getPDF(const Intersection&,
			const Intersection&) = 0;



Each of the sub-classes are defining the above functions.

There is another class called AreaLight that contains a std::vector as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
   class LuminaireProbability
   {
   public:

       LuminaireProbability() : mIntersectable(0),mMaterial(0),mLuminaireProbabilityValue(0.0f),
           mCumulativeDistributionValue(0.0f)
       {
       }

       ~LuminaireProbability()
       {
           mIntersectable = 0;
           mMaterial = 0;
       }

      //luminaire as an intersectable
      Intersectable *mIntersectable;
      //material of the intersectable
      Material *mMaterial;
      //probability of choosing the luminaire
      float mLuminaireProbabilityValue;

      //cumulative distribution value
      float mCumulativeDistributionValue;

   };

   //the area light contains the reference
   //to the geometric object to be the light source
   //the geometric object in turn contains the emissive material
   //thus providing the illumination
   //in this way the type of geometric object automatically determines
   //the type of light, in principle it allows any type of the geometric
   //object to be a light source
   //SOME OF THE ABOVE STATEMENTS DEMANS CHANGE
   std::vector<LuminaireProbability*> mLightIntersectables;

   //store the selected lumianire from the vector
   LuminaireProbability *mSelectedLuminaire;


I have the std::vector populated with a pointer of intersectables of type triangle or sphere along with some other trivial information. Now i have the following function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//calcualate the world position based on the intersection point
bool AreaLight::calculateSampleDirection(const Intersection &intersection,Vector3D &sampleVector)
{

   //choose the light intersectables based on the weight of the probaiblity
   //that we have calculated in the prepare() function

   float selectionProbability = 0.0f;

   //create a random instance
   RandomGenerator random = RandomGenerator::getRandomInstance();

   //create a canonical random number between 0 and 1
   selectionProbability = static_cast<float>(random.generateDouble(0,1));

   CompareToCDF cdf;
   //store the canonical random value
   cdf.mRandomValue = selectionProbability;

//   CompareAreaLight cmpAreaLight;

//   //sort the light intersectables based on the luminaire probability value
//   std::sort(mLightIntersectables.begin(),mLightIntersectables.end(),cmpAreaLight);

   std::vector<LuminaireProbability*>::iterator it;

   it = std::find_if(mLightIntersectables.begin(),mLightIntersectables.end(),cdf);

   assert(it != mLightIntersectables.end());

   //store the selected luminaire
   mSelectedLuminaire = *it;

   //I AM GETTING A SEGMENTATION FAULT IN THE FOLLOWING FUNCTION - !!!!!
   mSelectedLuminaire->mIntersectable->sampleDirection(intersection,sampleVector);

   return true;
}



As mentioned in the code i am getting the segmentation fault that the debugger is not even giving any hint of.


I hope you will be able to help me find out the issue here that i am missing.
Is there any logical mistake i am doing here?


Regards
Sajjad
Obviously you have a pretty complex program here, so it would be difficult if not impossible for anyone to diagnose the issue with only what you gave us. But if I had to guess, I would say that since your constructor sets all of your pointers to 0 that one of the objects being referenced is still pointing to a NULL value. Maybe temporarily try something like:
1
2
3
4
5
6
7
if(mSelectedLuminaire != NULL)
{
    if(mSelectedLuminaire->mIntersectable != NULL(
    {
         //code code code 
    }
}
Thanks for the hint. I tried as follows:

1
2
3
4
5
6
   if(mSelectedLuminaire != 0)
    {
       if(mSelectedLuminaire->mIntersectable != 0)
        //SEGMENTATION FAULT!!!
        mSelectedLuminaire->mIntersectable->sampleDirection(intersection,sampleVector);
    }


Still i am getting the segmentation fault with the function as before.

Any where to look into?

Regards
Sajjad
Just so that we know for sure, would you mind copy and pasting the error that you are getting?
Hi again,

I get the following while running the application:

1
2
3
4
5
6
7
8
9
10
11
12
Path tracing...
[New Thread 0xb517bb40 (LWP 4525)]
[New Thread 0xb497ab40 (LWP 4526)]
[New Thread 0xb4179b40 (LWP 4527)]
[New Thread 0xb3978b40 (LWP 4528)]
[New Thread 0xb3177b40 (LWP 4529)]
[New Thread 0xb2976b40 (LWP 4530)]
[New Thread 0xb2175b40 (LWP 4531)]

Program received signal SIGSEGV, Segmentation fault.
0x2efd85ba in ?? ()



And then i tried to run with GDB, did the backtrace and then i get the following :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(gdb) backtrace
#0  0x2efd85ba in ?? ()
#1  0x0804e018 in AreaLight::calculateSampleDirection (this=0x80c4d60, 
    intersection=..., sampleVector=...) at arealight.cpp:151
#2  0x080556ed in Intersection::getShadowRay (this=0xbfffedfc, light=0x80c4d60)
    at intersection.cpp:152
#3  0x08069787 in PathTracer::computeDirectIllumination (this=0xbffff220, 
    intersection=...) at pathtracer.cpp:179
#4  0x0806a3df in PathTracer::computeRadiance (this=0xbffff220, 
    intersection=..., depth=5) at pathtracer.cpp:141
#5  0x0806a63f in PathTracer::trace (this=0xbffff220, ray=..., E=1)
    at pathtracer.cpp:127
#6  0x0806a8e0 in PathTracer::tracePixel (this=0xbffff220, x=0, y=1)
    at pathtracer.cpp:95
#7  0x0806aa14 in PathTracer::computeImage() [clone ._omp_fn.0] ()
    at pathtracer.cpp:53
#8  0x0806ac46 in PathTracer::computeImage (this=0xbffff220)
    at pathtracer.cpp:47
#9  0x0804c397 in main (argc=1, argv=0xbffff304) at main.cpp:504
(gdb) 


Please let me know if there is any more information i can provide you with to help me get around with this issue.

Thanks
Sajjad
Are you using multiple threads or am I reading that wrong? Maybe something is over-writing\deleting one of your const references while another thread is trying to use it?
I am not using any multi-threading over here. Where do think i am deleting stuff in the code i have provided with you so far.

I am just cleaning the memory occupied in the std::vector in the destructor for the AreaLight as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
AreaLight::~AreaLight()
{
   //empty the vector
    for(unsigned int i = 0; i < mLightIntersectables.size();++i)
    {
        delete mLightIntersectables[i];
        mLightIntersectables[i] = 0;
    }

   mLightIntersectables.clear();

   if(mSelectedLuminaire != 0)
   {
       delete mSelectedLuminaire;
       mSelectedLuminaire = 0;
   }
 
}



Anything more ?
Topic archived. No new replies allowed.