Reputation: 2615
I have following code:
class RawModel{
public:
RawModel(GLuint id, GLuint count);
GLuint* getID(void);
GLuint* getVertexCount(void);
private:
GLuint *vaoID;
GLuint *verts;
};
RawModel::RawModel(GLuint id, GLuint count){
*vaoID = id;
*verts = count;
}
GLuint* RawModel::getID(void){
return vaoID;
}
GLuint* RawModel::getVertexCount(void){
return verts;
}
But upon running, it crashes. If I don't create an instance of this class, my Application runs fine, but the Constructor seems to be crashing, and I don't know why. (I'm fairly new to C++).
I need to use pointers, because of the function glDeleteVertexArrays()
calling for a Pointer as a 2nd parameter.
Upvotes: 0
Views: 171
Reputation: 44258
If you need to initialize pointers, initialize them, not data they point to:
RawModel::RawModel(GLuint id, GLuint count){
vaoID = nullptr;
verts = nullptr;
}
but even better use member initialization:
RawModel::RawModel(GLuint id, GLuint count) :
vaoID( nullptr ),
verts( nullptr )
{
}
but it is not clear why you have pointers in class and return pointers in methods, instead of storing and returning them by value.
Upvotes: 1
Reputation: 122476
This
*vaoID = 0;
is not initializing a pointer. Instead it is dereferencing a pointer and trying to assing a zero to the pointee. However, there is no pointee. Dereferencing a pointer that does not point to something is undefined behaviour.
Initializing the pointer would be
vaoID = nullptr;
But note that after this the pointer still points to nothing. You will have to create an object to make the pointer point to it.
Btw it is not clear why you use pointers in the first place. I dont know that lib, but most likely you can simply have instances as members:
class RawModel{
//...
private:
GLuint vaoID;
GLuint verts;
};
Upvotes: 1