Jump to content
  • Advertisement
Sign in to follow this  
R4v3nh34rt

Unity [C++] Application crashes

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings everyone, I just joined the community and hopefully asking a question right-away as my first post won't appear rude.
I'll get straight to the point: I'm new at C++ programming , I actually started programming with the latter. I had grasped the basics of programming with C++ , but of course, I hadn't learnt nor mastered the complexity of the language as it takes years to do that as I'm sure many of you experienced programmers know. Then I sticked with other , easier languages because I thought they were the most appropriate to my needs: Python, pawn, some little games in assembly just to challenge myself.
After all of this, the result is that when reverting back to C++, the difficulties when it came to making some things work were just unexplainable to me.
For instance, I made this program using the opengl libraries, it's going to be a game. I'm basically making a little engine from scratch just to make a simple game out of it, it's not going to be a lifetime project, but merely a practicing project.
So any unrelated tips regarding the style of programming I've adopted should be avoided, as I'm sure we'll be able to discuss that at a later time.
The main problem with my program is that when I create the object inside a function and then assign the object to an array, it crashes the program.

The following is the main part of the code, If i'm not mistaken it's the only one that really counts in order to solve the issue:


Entity* entityList[MAX_ENTITIES];

Entity* createEntity()
{

Point p1(2.0,1.0,-2.0,true);
Point p2(2.5,1.0,-2.0,true);
Point p3(2.5,1.5,-2.0,true);
Point p4(2.0,1.5,-2.0,true);
Line l1(p1,p2,false);
Line l2(p2,p3,true);
Line l3(p3,p4,true);
Line l4(p4,p1,true);
Rectshape r1(l1,l2,l3,l4,false);
Point p5(1.0,1.0,-2.0,true);
Point p6(1.5,1.0,-2.0,true);
Point p7(1.5,1.5,-2.0,true);
Point p8(1.0,1.5,-2.0,true);
Line l5(p5,p6,true);
Line l6(p6,p7,true);
Line l7(p7,p8,true);
Line l8(p8,p5,true);
Rectshape r2(l5,l6,l7,l8,false);
Point p9(3.0,1.0,-2.0);
Point p10(3.5,1.0,-2.0);
Point p11(3.5,1.5,-2.0);
Point p12(3.0,1.5,-2.0);
Line l9(p9,p10,true);
Line l10(p10,p11,true);
Line l11(p11,p12,true);
Line l12(p12,p9,true);
Rectshape r3(l9,l10,l11,l12,false);
Point p13(3.0,1.0,-2.0,true);
Point p14(3.5,1.0,-2.0,true);
Point p15(3.5,1.5,-2.0,true);
Point p16(3.0,1.5,-2.0,true);
Line l13(p13,p14,true);
Line l14(p14,p15,true);
Line l15(p15,p16,true);
Line l16(p16,p13,true);
Rectshape r4(l13,l14,l15,l16,false);
Point p17(3.0,1.0,-2.0,true);
Point p18(3.5,1.0,-2.0,true);
Point p19(3.5,1.5,-2.0,true);
Point p20(3.0,1.5,-2.0,true);
Line l17(p17,p18,true);
Line l18(p18,p19,true);
Line l19(p19,p20,true);
Line l20(p20,p17,true);
Rectshape r5(l17,l18,l19,l20,false);
Point p21(13.0,1.0,-2.0,true);
Point p22(13.5,1.0,-2.0,true);
Point p23(13.5,1.5,-2.0,true);
Point p24(13.0,1.5,-2.0,true);
Line l21(p21,p22,true);
Line l22(p22,p23,true);
Line l23(p23,p24,true);
Line l24(p24,p21,true);
Rectshape r6(l21,l22,l23,l24,false);
CubeSolid *c1 = new CubeSolid(r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(*c1,ENTITY_DYNAMIC_TYPE);
return e1;
}
int main (int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);

glutInitWindowSize(HEIGHT, WIDTH);

glutInitWindowPosition(100, 50);
glViewport(0, 0, WIDTH, HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glutCreateWindow("Mofo");
init();


glutDisplayFunc(display);
glutPassiveMotionFunc(getMousePosition);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,0.0);

entityList[0]=createEntity();// CRASHES THE PROGRAM

for (int e = 0; e < 1; e++)
{
if (entityList[e])
{
entityList[e]->spawn(true);

if (entityList[e]->type == ENTITY_DYNAMIC_TYPE)
{
int threadIndex = getIdleThreadIndex();
if (threadIndex > -1)
{
//HANDLE threadHandles[threadIndex] = e
threadExecuted[threadIndex] = 1;
threadDelay[threadIndex] = 50;
threadedEntities[threadIndex] = entityList[e];
threadedFunctions[threadIndex] = FUNC_MOVETO;

CreateThread(NULL, 0, gameThread, &threadIndex, 0, NULL);
}
}
}
}
glutIdleFunc(idle);
glutMainLoop();

}





For some reason it doesn't happen when I don't use the createEntity function, and just put everything in main. The following code works:




int main (int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);

glutInitWindowSize(HEIGHT, WIDTH);
glutInitWindowPosition(100, 50);
glViewport(0, 0, WIDTH, HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glutCreateWindow("Mofo");
init();
glutDisplayFunc(display);
glutPassiveMotionFunc(getMousePosition);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,0.0);
Point p1(2.0,1.0,-2.0,true);
Point p2(2.5,1.0,-2.0,true);
Point p3(2.5,1.5,-2.0,true);
Point p4(2.0,1.5,-2.0,true);
Line l1(p1,p2,false);
Line l2(p2,p3,true);
Line l3(p3,p4,true);
Line l4(p4,p1,true);
Rectshape r1(l1,l2,l3,l4,false);
Point p5(1.0,1.0,-2.0,true);
Point p6(1.5,1.0,-2.0,true);
Point p7(1.5,1.5,-2.0,true);
Point p8(1.0,1.5,-2.0,true);
Line l5(p5,p6,true);
Line l6(p6,p7,true);
Line l7(p7,p8,true);
Line l8(p8,p5,true);
Rectshape r2(l5,l6,l7,l8,false);
Point p9(3.0,1.0,-2.0);
Point p10(3.5,1.0,-2.0);
Point p11(3.5,1.5,-2.0);
Point p12(3.0,1.5,-2.0);
Line l9(p9,p10,true);
Line l10(p10,p11,true);
Line l11(p11,p12,true);
Line l12(p12,p9,true);
Rectshape r3(l9,l10,l11,l12,false);
Point p13(3.0,1.0,-2.0,true);
Point p14(3.5,1.0,-2.0,true);
Point p15(3.5,1.5,-2.0,true);
Point p16(3.0,1.5,-2.0,true);
Line l13(p13,p14,true);
Line l14(p14,p15,true);
Line l15(p15,p16,true);
Line l16(p16,p13,true);
Rectshape r4(l13,l14,l15,l16,false);
Point p17(3.0,1.0,-2.0,true);
Point p18(3.5,1.0,-2.0,true);
Point p19(3.5,1.5,-2.0,true);
Point p20(3.0,1.5,-2.0,true);
Line l17(p17,p18,true);
Line l18(p18,p19,true);
Line l19(p19,p20,true);
Line l20(p20,p17,true);
Rectshape r5(l17,l18,l19,l20,false);
Point p21(13.0,1.0,-2.0,true);
Point p22(13.5,1.0,-2.0,true);
Point p23(13.5,1.5,-2.0,true);
Point p24(13.0,1.5,-2.0,true);
Line l21(p21,p22,true);
Line l22(p22,p23,true);
Line l23(p23,p24,true);
Line l24(p24,p21,true);
Rectshape r6(l21,l22,l23,l24,false);
CubeSolid *c1 = new CubeSolid(r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(*c1,ENTITY_DYNAMIC_TYPE);

entityList[0]=e1;

for (int e = 0; e < 1; e++)
{
if (entityList[e])
{
entityList[e]->spawn(true);

if (entityList[e]->type == ENTITY_DYNAMIC_TYPE)
{
int threadIndex = getIdleThreadIndex();
if (threadIndex > -1)
{
//HANDLE threadHandles[threadIndex] = e
threadExecuted[threadIndex] = 1;
threadDelay[threadIndex] = 50;
threadedEntities[threadIndex] = entityList[e];
threadedFunctions[threadIndex] = FUNC_MOVETO;

CreateThread(NULL, 0, gameThread, &threadIndex, 0, NULL);
}
}
}
}
glutIdleFunc(idle);
glutMainLoop();



}



Ignore the thread cycle and other stuff which is unrelated to the issue, I tested it.
Thanks for your help, time and attention :).

Share this post


Link to post
Share on other sites
Advertisement
entityList[0] = createEntity();

And what type, pray tell, is entityList? is it

a: Entity** entityList;
b: std::vector<Entity*> entityList, or
c: Entity* entityList[ SOME_NUM ]

If it's a, you haven't allocated any memory for it.
If it's b, you haven't called resize (or push_back/insert)
If it's c, uhm.....

Share this post


Link to post
Share on other sites
Sorry, I missed the global declaration when I pasted. Thanks for giving multiple answers.
here's the global declaration:
Entity* entityList[MAX_ENTITIES];

So unfortunatly it fits into your "C" answer, damn it :(.
It's really weird as it seems to work outside the function "createEntity" with the exact same code, but once inside the function it just crashes the program. This is odd.I also traced the error and it's definetly that assignment that causes it.

Share this post


Link to post
Share on other sites
Hidden

Greetings everyone, I just joined the community and hopefully asking a question right-away as my first post won't appear rude.
I'll get straight to the point: I'm new at C++ programming , I actually started programming with the latter. I had grasped the basics of programming with C++ , but of course, I hadn't learnt nor mastered the complexity of the language as it takes years to do that as I'm sure many of you experienced programmers know. Then I sticked with other , easier languages because I thought they were the most appropriate to my needs: Python, pawn, some little games in assembly just to challenge myself.
After all of this, the result is that when reverting back to C++, the difficulties when it came to making some things work were just unexplainable to me.
For instance, I made this program using the opengl libraries, it's going to be a game. I'm basically making a little engine from scratch just to make a simple game out of it, it's not going to be a lifetime project, but merely a practicing project.
So any unrelated tips regarding the style of programming I've adopted should be avoided, as I'm sure we'll be able to discuss that at a later time.
The main problem with my program is that when I create the object inside a function and then assign the object to an array, it crashes the program.

The following is the main part of the code, If i'm not mistaken it's the only one that really counts in order to solve the issue:


Entity* createEntity()
{

Point p1(2.0,1.0,-2.0,true);
Point p2(2.5,1.0,-2.0,true);
Point p3(2.5,1.5,-2.0,true);
Point p4(2.0,1.5,-2.0,true);
Line l1(p1,p2,false);
Line l2(p2,p3,true);
Line l3(p3,p4,true);
Line l4(p4,p1,true);
Rectshape r1(l1,l2,l3,l4,false);
Point p5(1.0,1.0,-2.0,true);
Point p6(1.5,1.0,-2.0,true);
Point p7(1.5,1.5,-2.0,true);
Point p8(1.0,1.5,-2.0,true);
Line l5(p5,p6,true);
Line l6(p6,p7,true);
Line l7(p7,p8,true);
Line l8(p8,p5,true);
Rectshape r2(l5,l6,l7,l8,false);
Point p9(3.0,1.0,-2.0);
Point p10(3.5,1.0,-2.0);
Point p11(3.5,1.5,-2.0);
Point p12(3.0,1.5,-2.0);
Line l9(p9,p10,true);
Line l10(p10,p11,true);
Line l11(p11,p12,true);
Line l12(p12,p9,true);
Rectshape r3(l9,l10,l11,l12,false);
Point p13(3.0,1.0,-2.0,true);
Point p14(3.5,1.0,-2.0,true);
Point p15(3.5,1.5,-2.0,true);
Point p16(3.0,1.5,-2.0,true);
Line l13(p13,p14,true);
Line l14(p14,p15,true);
Line l15(p15,p16,true);
Line l16(p16,p13,true);
Rectshape r4(l13,l14,l15,l16,false);
Point p17(3.0,1.0,-2.0,true);
Point p18(3.5,1.0,-2.0,true);
Point p19(3.5,1.5,-2.0,true);
Point p20(3.0,1.5,-2.0,true);
Line l17(p17,p18,true);
Line l18(p18,p19,true);
Line l19(p19,p20,true);
Line l20(p20,p17,true);
Rectshape r5(l17,l18,l19,l20,false);
Point p21(13.0,1.0,-2.0,true);
Point p22(13.5,1.0,-2.0,true);
Point p23(13.5,1.5,-2.0,true);
Point p24(13.0,1.5,-2.0,true);
Line l21(p21,p22,true);
Line l22(p22,p23,true);
Line l23(p23,p24,true);
Line l24(p24,p21,true);
Rectshape r6(l21,l22,l23,l24,false);
CubeSolid *c1 = new CubeSolid(r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(*c1,ENTITY_DYNAMIC_TYPE);
return e1;
}
int main (int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);

glutInitWindowSize(HEIGHT, WIDTH);

glutInitWindowPosition(100, 50);
glViewport(0, 0, WIDTH, HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glutCreateWindow("Mofo");
init();


glutDisplayFunc(display);
glutPassiveMotionFunc(getMousePosition);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,0.0);

entityList[0]=createEntity();// CRASHES THE PROGRAM

for (int e = 0; e < 1; e++)
{
if (entityList[e])
{
entityList[e]->spawn(true);

if (entityList[e]->type == ENTITY_DYNAMIC_TYPE)
{
int threadIndex = getIdleThreadIndex();
if (threadIndex > -1)
{
//HANDLE threadHandles[threadIndex] = e
threadExecuted[threadIndex] = 1;
threadDelay[threadIndex] = 50;
threadedEntities[threadIndex] = entityList[e];
threadedFunctions[threadIndex] = FUNC_MOVETO;

CreateThread(NULL, 0, gameThread, &threadIndex, 0, NULL);
}
}
}
}
glutIdleFunc(idle);
glutMainLoop();

}





For some reason it doesn't happen when I don't use the createEntity function, and just put everything in main. The following code works:




int main (int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);

glutInitWindowSize(HEIGHT, WIDTH);
glutInitWindowPosition(100, 50);
glViewport(0, 0, WIDTH, HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glutCreateWindow("Mofo");
init();
glutDisplayFunc(display);
glutPassiveMotionFunc(getMousePosition);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,0.0);
Point p1(2.0,1.0,-2.0,true);
Point p2(2.5,1.0,-2.0,true);
Point p3(2.5,1.5,-2.0,true);
Point p4(2.0,1.5,-2.0,true);
Line l1(p1,p2,false);
Line l2(p2,p3,true);
Line l3(p3,p4,true);
Line l4(p4,p1,true);
Rectshape r1(l1,l2,l3,l4,false);
Point p5(1.0,1.0,-2.0,true);
Point p6(1.5,1.0,-2.0,true);
Point p7(1.5,1.5,-2.0,true);
Point p8(1.0,1.5,-2.0,true);
Line l5(p5,p6,true);
Line l6(p6,p7,true);
Line l7(p7,p8,true);
Line l8(p8,p5,true);
Rectshape r2(l5,l6,l7,l8,false);
Point p9(3.0,1.0,-2.0);
Point p10(3.5,1.0,-2.0);
Point p11(3.5,1.5,-2.0);
Point p12(3.0,1.5,-2.0);
Line l9(p9,p10,true);
Line l10(p10,p11,true);
Line l11(p11,p12,true);
Line l12(p12,p9,true);
Rectshape r3(l9,l10,l11,l12,false);
Point p13(3.0,1.0,-2.0,true);
Point p14(3.5,1.0,-2.0,true);
Point p15(3.5,1.5,-2.0,true);
Point p16(3.0,1.5,-2.0,true);
Line l13(p13,p14,true);
Line l14(p14,p15,true);
Line l15(p15,p16,true);
Line l16(p16,p13,true);
Rectshape r4(l13,l14,l15,l16,false);
Point p17(3.0,1.0,-2.0,true);
Point p18(3.5,1.0,-2.0,true);
Point p19(3.5,1.5,-2.0,true);
Point p20(3.0,1.5,-2.0,true);
Line l17(p17,p18,true);
Line l18(p18,p19,true);
Line l19(p19,p20,true);
Line l20(p20,p17,true);
Rectshape r5(l17,l18,l19,l20,false);
Point p21(13.0,1.0,-2.0,true);
Point p22(13.5,1.0,-2.0,true);
Point p23(13.5,1.5,-2.0,true);
Point p24(13.0,1.5,-2.0,true);
Line l21(p21,p22,true);
Line l22(p22,p23,true);
Line l23(p23,p24,true);
Line l24(p24,p21,true);
Rectshape r6(l21,l22,l23,l24,false);
CubeSolid *c1 = new CubeSolid(r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(*c1,ENTITY_DYNAMIC_TYPE);

entityList[0]=e1;

for (int e = 0; e < 1; e++)
{
if (entityList[e])
{
entityList[e]->spawn(true);

if (entityList[e]->type == ENTITY_DYNAMIC_TYPE)
{
int threadIndex = getIdleThreadIndex();
if (threadIndex > -1)
{
//HANDLE threadHandles[threadIndex] = e
threadExecuted[threadIndex] = 1;
threadDelay[threadIndex] = 50;
threadedEntities[threadIndex] = entityList[e];
threadedFunctions[threadIndex] = FUNC_MOVETO;

CreateThread(NULL, 0, gameThread, &threadIndex, 0, NULL);
}
}
}
}
glutIdleFunc(idle);
glutMainLoop();



}



Ignore the thread cycle and other stuff which is unrelated to the issue, I tested it.
Thanks for your help, time and attention :).

Share this post


Link to post
The obvious question would be, is there anything relevant when it crashes, that could shed some light on this?

I, because I have no life :P, created a stripped down version, and it compiled fine.





#include <iostream>

#define MAX_ENTITIES 10

class Entity
{
public:
std::string m_name;
Entity (std::string name) { m_name = name; }
std::string MyName () { return m_name; }
};

Entity* createEntity()
{
Entity *e1;
e1 = new Entity("R4v3nh34rt");
return e1;
}

Entity* entityList[MAX_ENTITIES];

int main (int argc, char *argv[])
{
entityList[0]=createEntity();// CRASHES THE PROGRAM
std::string name = entityList[0]->MyName();

std::cout << "hello " << name.c_str() << std::endl;

if (entityList[0] != NULL) { delete (entityList[0]); }


// bad practice, but fast
int n;
std::cin >> n;
}


That leads me to think it's not an issue with your assignment but, rather, somewhere else.

Note: compiled with Visual Studio 2010

Share this post


Link to post
Share on other sites
From the forum FAQ and Rules (linked at the top of this page):

If you have encountered an error, such as a compiler error or a runtime crash message, please provide the complete and exact text of the message when posting. Paraphrasing or omitting details is not helpful. Often these errors provide subtle but important clues as to the origins of a problem, and more experienced members may be able to help decipher the messages and locate a solution.

...

If you are asking for help with a piece of code, especially for debugging or troubleshooting purposes, please reduce the code as far as you can to a minimum sample that reproduces your problem. For instance, if you have a syntax error, post the smallest piece of code you can that triggers the syntax error, and no more. If you have commented out code for testing or experimentation purposes, please either omit it when posting, or clearly explain the purpose of the commented-out sections. This helps reduce ambiguity and makes it faster and easier for others to assist you.[/quote]


Please follow these guidelines. It'll make it a lot easier for us to help you out.

Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!