PDA

View Full Version : gluSphere crashes if GLUquadric is member pointer



Fejimush
06-08-2008, 04:08 PM
This works fine:

void draw_sphere()
{
GLUquadric* m_sphere_quad = gluNewQuadric();
glColor3f(1.0, 1.0, 0.0);
gluSphere(m_sphere_quad, 30.0, 15, 15);
gluDeleteQuadric(m_sphere_quad);
}

This crashes on gluSphere() call:

// sphere.h
class sphere
{
GLUquadric *m_quad;
public:
sphere();
~sphere();
void draw();
};

// sphere.cpp
sphere::sphere()
{
m_quad = gluNewQuadric();
}

void sphere::draw()
{
glColor3f(1.0, 1.0, 0.0);
gluSphere(m_quad, 30.0, 15, 15); // Crashes even though m_quad != NULL
}

Is there any reason why the GLUquadric pointer can't be a member of a class?

thanks,
graham

CatDog
06-08-2008, 06:23 PM
No.
At first glance, this looks like a bug in your calling code: m_quad seems to be invalid when gluSphere() is called. Maybe you delete the sphere object by mistake and call its draw() method afterwards?

CatDog

Jon723
06-08-2008, 09:41 PM
For testing sake, in your draw function add an if statement that test's to see if you have a valid pointer and place a breakpoint there.

Fejimush
06-08-2008, 10:27 PM
Ok thanks. At least I know it should work. I must be doing something wonky.

graham