gluSphere crashes if GLUquadric is member pointer

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

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

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.

Ok thanks. At least I know it should work. I must be doing something wonky.

graham