Hello everyone, I just joined the forums and I'm glad to see there is a specialized community that knows more than me about C++.
I'll get straight to the point and I don't want to appear rude . I'm not here to just ask and then leave :).
I'm making a small game engine in OpenGL, and the issue seems related to objects, arrays and pointers.
Basically I have the following global array:
Entity **entityList = new Entity*[ MAX_ENTITIES ];
and whenever I have it outside of main, it crashes the program. Let's be more specific.
If I compile something like this and run it, it will work just fine without problems:
1 2 3 4 5 6 7 8 9 10 11
|
int main (int argc, char *argv[])
{
CubeSolid c1 (r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(c1,ENTITY_DYNAMIC_TYPE);
entityList[0]=e1;
}
|
Now, If i wanted to do the same by calling a function with the same block of code in its body, and then call one of Entity methods outside main,
it will crash the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void createEntity()
{
CubeSolid c1 (r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(c1,ENTITY_DYNAMIC_TYPE);
entityList[0]=e1;
}
int main (int argc, char *argv[])
{
createEntity(); // THIS CRASHES THE PROGRAM WHENEVER ONE OF ENTITY METHODS IS USED OUTSIDE MAIN
}
|
As you can see, the program only crashes when the assignment to entityList gets done within createEntity() and a member function of an Entity object gets called. Read below for some examples.
I've tried almost anything: at first, I just had an array of pointers rather than pointers of pointers, still the same problem. I also had tried both dynamic and static memory allocations. No success, same problem.
Just why wouldn't it work when inside a function? What's the big deal with that? I know, I'm not thinking low-level enough, still It's my first , real experience with C++.
I also wanted to post more relevant code, the class headers for instance:
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
// THIS IS Entity.h
class Entity
{
public:
Entity();
Entity(PrimitiveSolid &b,int t);
Entity(const Entity &e);
//Entity& operator=(const Entity& e);
PrimitiveSolid* body;
int type;
void spawn(bool status);
void setVelocity(GLfloat vX, GLfloat vY, GLfloat vZ);
void moveTo(GLfloat x, GLfloat y, GLfloat z, GLfloat speed);
void getPosition(GLfloat &x, GLfloat &y, GLfloat &z);
bool isInRangeOfPoint(GLfloat range, GLfloat x, GLfloat y, GLfloat z);
bool isSpawned();
virtual ~Entity();
protected:
private:
bool spawned;
};
// THIS IS PrimitiveSolid.h
class PrimitiveSolid
{
public:
PrimitiveSolid();
bool visible;
bool wireMode;
virtual void draw();
virtual void arrangeShape();
virtual void setScale(GLfloat scale);
virtual void setPos(GLfloat cX, GLfloat cY, GLfloat cZ);
virtual void rotate(GLfloat rX, GLfloat rY, GLfloat rZ);
virtual void rotateDeg(GLfloat angleX, GLfloat angleY, GLfloat angleZ);
virtual GLfloat getScale();
virtual GLfloat getPosX();
virtual GLfloat getPosY();
virtual GLfloat getPosZ();
virtual ~PrimitiveSolid();
protected:
private:
};
class CubeSolid : public PrimitiveSolid
{
public:
CubeSolid(Rectshape &r1, Rectshape &r2, Rectshape &r3, Rectshape &r4,Rectshape &r5, Rectshape &r6,bool hidden, bool wireMode);
CubeSolid(GLfloat size,bool hidden, bool wireMode);
Rectshape* faces[6];
void draw();
void arrangeShape();
void setScale(GLfloat scale);
void setPos(GLfloat cX, GLfloat cY, GLfloat cZ);
void rotate(GLfloat rX, GLfloat rY, GLfloat rZ);
void rotateDeg(GLfloat angleX, GLfloat angleY, GLfloat angleZ);
GLfloat getScale();
GLfloat getPosX();
GLfloat getPosY();
GLfloat getPosZ();
virtual ~CubeSolid();
protected:
private:
GLfloat scale;
GLfloat oPos[3];
};
|
I've also noticed that the program crashes when a function gets called by accessing a member function of the body member.
|
entityList[0]->body->draw();
|
But it doesn't seem to crash when I access a member variable:
entityList[0]->body->visible;
Again, all of this happens only when the assignment takes place within createEntity() or any other function as far as I know. When the assignment gets done in main, the program never crashes.
Something else worth noting: It doesn't seem to crash at all when the methods are called inside main. Again, this seems to be some kind of scope problem.
Also, since I have Visual Studio installed (but currently, I'm not using it to program), whenever the application encounters a runtime error the debugger opens and it tells me "access violation". Now I'm not sure that 's an error with the debugger or the runtime error that was encountered as I'm not familiar with visual studio.
I'm currently using MINGW and codeblocks.
The problem doesn't seem to be in the Entity class, it's the member body that causes problems with its methods.
For instance, the call entityList[0]->isSpawned(); won't crash the program, because it has nothing to do with "body". But anything related to body crashes the whole thing.
Thanks for any advice or help I can get, please do help on the current issue, the style I've adopted for my project isn't an issue I wish to address now, but any help related to the problem addressed here will be appreciated!
Thank you.