Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Program terminates on error

This thread is locked; no one can reply to it. rss feed Print
Program terminates on error
adhrymes
Member #6,567
November 2005

Hey guys,
this is my first game ive done and I'm getting some wierd error on termination, its one of the "would you like to send an error report" ones. Everything was fine untill i added threads but now that I added them I get this. I was hoping you guys would be able to help me out. I attached the compiled files so you could see what was happening.
Theres too many files in the project some of which im 99% sure are bug free(hahah)
so i'll just paste in the new class which adds the threads and the main func that calls it.
Also, in this code i origonally intended to run a thread to update the sprites and then another one to do the drawing but then i ended up combining them so thats why there those extra var's/funcs. but i just wanted to see how other people were threading there programs so any input is appreciated.

EDIT:
PS q/w a/s z/x AREN'T threadsafe so dont hit them, hehe
-----

A side note: it's been too long since my algorithms class and I forget how STL allocates memory. im always someone who likes doing everything himself but im foring myself to use STL here cause i know once i get used to it i'll like it.

anyway so now when packing my vectors im doing
vector<sprite> vec;
sprite temp;
for()//inside some for-loop
{
temp.set(blah);//sets attributes
vec.push_back(temp);
}
instead of using new since i've found it seems like when you push something memory is allocated and temp is copied in, so ide be leaking if i used new. but im not sure if this is right. anyone?

END EDIT

thanks
ad

1extern "C" void * spHelper(void*);
2extern "C" void * rHelper(void*);
3 
4class scheduler {
5 
6 
7 public:
8 scheduler();
9 ~scheduler();
10 void start();
11 void stop();
12 void update(){sp.update();};
13 void *spriteThread(void* data);
14 void *rendererThread(void* data);
15 void drawScreen();
16
17 private:
18 bool done;
19 background bg;
20 layer fg;
21 spriteHandler sp;
22 BITMAP *buffer;
23 int x,tId,tId2,id1,id2;
24 double lastT;
25 pthread_t thread0;
26 pthread_t thread1;
27 
28 
29 
30};
31 
32scheduler::scheduler()
33{
34 bg.set("city",2);
35 fg.set("city.BMP","cityFG.CSV");
36 sp.set("citySP.CSV","cityC.CSV","sprites.CSV");
37 buffer = create_bitmap_ex(DEPTH,WIDTH,HEIGHT);
38 x=0,tId=0,tId2=1,id1=0;id2=0;
39 lastT=0;
40 done=false;
41 clear_to_color(buffer,TRANS);
42 blit(buffer,screen,0,0,0,0,WIDTH,HEIGHT);
43}
44 
45scheduler::~scheduler()
46{
47 destroy_bitmap(buffer);
48}
49 
50void scheduler::start()
51{
52// id1= pthread_create(&thread0,NULL,spHelper,this);
53 id2= pthread_create(&thread1,NULL,rHelper,this);
54}
55 
56void scheduler::stop()
57{
58 done = true;
59}
60 
61 
62void scheduler::drawScreen()
63{
64 bg.getScreen(buffer,x,0);
65 sp.getScreen(buffer,x,0);
66 fg.getLayer(buffer,x,0);
67 
68 vsync();
69 acquire_screen();
70 masked_blit(buffer,screen,0,0,0,0,WIDTH,HEIGHT);
71 release_screen();
72 
73}
74 
75void *spHelper(void *obj)
76{
77 scheduler *Obj = (scheduler *)obj;
78 Obj->spriteThread(NULL);
79 return 0;
80}
81 
82void *rHelper(void *obj)
83{
84 scheduler *Obj = (scheduler *)obj;
85 Obj->rendererThread(NULL);
86 return 0;
87}
88 
89void *scheduler::spriteThread(void* data)
90{
91 
92// int my_thread_id = *((int*)data);
93 while(!done){
94 sp.update();
95 rest(RESTS);}
96 rest(10);
97 pthread_exit(NULL);
98 return NULL;
99}
100 
101void *scheduler::rendererThread(void* data)
102{
103// int my_thread_id = *((int*)data);
104 while(!done){
105 if( time(0)-lastT >= ((int)(1/(FPS))) ) {
106// sp.update();
107 if((player.getX()>= x+WIDTH-RIGHTWALL)&&(x<=MAPW-WIDTH-1)){ x+=STEP; }
108 if((player.getX()<= x+LEFTWALL)&&(x>=STEP) ){ x-=STEP; }
109 if(x<0){x=0;}
110 update();
111 drawScreen();
112 }
113 rest(RESTR);
114 }
115 rest(10);
116 pthread_exit(NULL);
117 return NULL;
118}

-----------------

1int main()
2{
3 allegro_init();
4 set_color_depth(DEPTH);
5 set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
6 install_keyboard();
7 
8 
9 scheduler game;
10 game.start();
11 
12 while(! key[KEY_ESC])
13 {
14 
15 if(key[KEY_RIGHT]){if(player.getX() <= MAPW-50) player.moveRight();}
16 if(key[KEY_LEFT]){if(player.getX()>= 1)player.moveLeft();}
17 if(key[KEY_DOWN]){player.duck();}
18 if(key[KEY_UP]){player.jump();}
19 if(key[KEY_A]) { JUMP+=1;}
20 if(key[KEY_S] ){ JUMP-=1;}
21 if(key[KEY_W] ){ GRAVITY-=1;}
22 if(key[KEY_Q] ){ GRAVITY+=1;}
23 if(key[KEY_Z] ){ STEP+=1;}
24 if(key[KEY_X] ){ STEP-=1;}
25 //if(key[KEY_SPACE]) { if(FIRECOUNT>=FIREDELAY) { player.ranged(); FIRECOUNT=0; } }
26// game.update();
27 //game.drawScreen();
28 }
29 game.stop();
30
31 //poll_keyboard();
32 rest(100);
33 allegro_exit();
34 rest(100);
35 return 0;
36}
37END_OF_MAIN();

CGamesPlay
Member #2,559
July 2002
avatar

Please click this link and use [code] tags.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

miran
Member #2,407
June 2002

I was told Allegro is not thread safe. Which means you can't do what you're trying to do. Which brings me to my first question. What are you trying to do and why do you have to use threads?

--
sig used to be here

adhrymes
Member #6,567
November 2005

thanks for the link to the text formatting thing.
anyway, this thread isn't that crucial but i was just kinda testing it out since i plan on putting the sound on a seporate thread but i just kinda wanted to test it out since i haven't used threads before, although i studied the theory.
but basically i thought i was terminating it incorrectly, but i guess not if you say allegro isn't threadsafe, so does this mean i have to do the sound another way now?

miran
Member #2,407
June 2002

I don't know how well Allegro works or doesn't work with threads. I just know that officially it's not thread safe, but that doesn't mean you can't get it to work. I don't know how though...

--
sig used to be here

CGamesPlay
Member #2,559
July 2002
avatar

I believe we can quote Kitty Cat saying something along the lines of:

Quote:

Allegro should wokr fine if you e.g. keep the sound in one thread and don't touch the sound anywhere else.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Billybob
Member #3,136
January 2003
avatar

You're destroying a bitmap in the destructor of that class. I'm not sure if Allegro will be shutdown by the time that occurs. If I'm right, Allegro will have shutdown before the destructor is called, hence you're calling an allegro function after Allegro has left the building.

If that is the case simply do:
scheduler *game = new scheduler();
And at the end
delete game;

EDIT: Actually I just noticed you do allegro_exit at the end, so yeah Allegro is shutdown before the destructor is called. I suggest doing what I said above and make sure you call delete before allegro_exit
EDIT2: Another option is to create Init and Cleanup functions which will do the work of the constructor and destructor. Then you can easily control when said functions are called.

scheduler game;
game.init();

/* bunch of stuff*/
game.cleanup();
allegro_exit();

_________________________________________________
"God speed, my lonely angel."
Bitcoin | Bitcoin Ponzi Scheme

A J
Member #3,025
December 2002
avatar

i use multiple threads with allegro.
but i have put all allegro functions into there own class. (a wrapper)
and then made that class threadsafe, its a little tedious to setup, but it does give you thread safety.

the other thing i use is a global uint32_t as a set of flags.. and set 'milestones' for startup/shutdown..
like:

glb_milestone |= ALLEGRO_START;
allegro_init();

then another thread can do things like:
if ( ALLEGRO_START & glb_milestone )
  can_do_allegro_stuff();

that why i can detect when each thread can/can-not processed.. its a primitve form of locking, but it works well, easy to implement.

___________________________
The more you talk, the more AJ is right. - ML

adhrymes
Member #6,567
November 2005

nice I manually destructed everything and it works great now, thanks alot. also i like that milestone idea and am going to implement it, thanks alot guys

Go to: