Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » game crashing on al_wait_for_event callee

Credits go to Peter Wang for helping out!
This thread is locked; no one can reply to it. rss feed Print
game crashing on al_wait_for_event callee
iam_donald
Member #14,318
May 2012
avatar

Hi,
my game's build crashes on one of the callees of al_wait_for_event. al_wait_for event is located in this function:

#SelectExpand
1void run_core(Core * core) 2 { 3 al_start_timer(core->timer); 4 Background * background = init_background("../frames/frames.json"); 5 while(not end_run) 6 { 7 ALLEGRO_EVENT event; 8 9 al_wait_for_event(core->queue, &event); 10 if(event.type == ALLEGRO_EVENT_TIMER) 11 { 12 /* update physics */ 13 draw_background(background->frame); 14 frame_step(background); 15 blit(); 16 } 17 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { end_run = true; } 18 else if(event.type == ALLEGRO_EVENT_KEY_DOWN) 19 { 20 ALLEGRO_KEYBOARD_STATE state; 21 al_get_keyboard_state(&state); 22 /* get input update */ 23 } 24 } 25 destroy_background(background); 26}

Prior to calling this function, I have calls to al_create_event_queue, and al_register_event_source for the queue,timer and keyboard respectively. I also assert that the calls succeeded by checking for NULL. Running it generates this trace:

Process: furi_debug [660]
Path: /Volumes/VOLUME/*/furi_debug
Identifier: furi_debug
Version: ??? (???)
Code Type: X86-64 (Native)
Parent Process: zsh [239]

Date/Time: 2012-05-25 18:08:27.306 +0000
OS Version: Mac OS X 10.7.4 (11E53)
Report Version: 9

Interval Since Last Report: 198172 sec
Crashes Since Last Report: 79
Per-App Crashes Since Last Report: 55
Anonymous UUID: 0987480B-D0A8-4583-9C35-A184280A4848

Crashed Thread: 5

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000097

VM Regions Near 0x97:
-->
__TEXT 000000010bb7e000-000000010bb80000 [ 8K] r-x/rwx SM=COW Volumes/VOLUME*

Application Specific Information:
objc[660]: garbage collection is OFF

Thread 0:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff8b5b467a mach_msg_trap + 10
1 libsystem_kernel.dylib 0x00007fff8b5b3d71 mach_msg + 73
2 com.apple.CoreFoundation 0x00007fff916fd50c __CFRunLoopServiceMachPort + 188
3 com.apple.CoreFoundation 0x00007fff91705c74 __CFRunLoopRun + 1204
4 com.apple.CoreFoundation 0x00007fff91705486 CFRunLoopRunSpecific + 230
5 com.apple.HIToolbox 0x00007fff8fe984d3 RunCurrentEventLoopInMode + 277
6 com.apple.HIToolbox 0x00007fff8fe9f781 ReceiveNextEventCommon + 355
7 com.apple.HIToolbox 0x00007fff8fe9f60e BlockUntilNextEventMatchingListInMode + 62
8 com.apple.AppKit 0x00007fff8a4b8e31 _DPSNextEvent + 659
9 com.apple.AppKit 0x00007fff8a4b8735 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
10 com.apple.AppKit 0x00007fff8a4b5071 -[NSApplication run] + 470
11 liballegro-debug.5.0.dylib 0x000000010bc70eb5 _al_osx_run_main + 3589 (osx_app_delegate.m:336)
12 liballegro_main-debug.5.0.dylib 0x000000010bd5ff2a main + 42 (osx_main.m:35)
13 furi_debug 0x000000010bb7ed34 start + 52

Thread 1:: Dispatch queue: com.apple.libdispatch-manager
0 libsystem_kernel.dylib 0x00007fff8b5b67e6 kevent + 10
1 libdispatch.dylib 0x00007fff8b0ba78a _dispatch_mgr_invoke + 923
2 libdispatch.dylib 0x00007fff8b0b931a _dispatch_mgr_thread + 54

Thread 2:
0 libsystem_kernel.dylib 0x00007fff8b5b6192 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff8b2ab594 _pthread_wqthread + 758
2 libsystem_c.dylib 0x00007fff8b2acb85 start_wqthread + 13

Thread 3:
0 libsystem_kernel.dylib 0x00007fff8b5b6192 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff8b2ab594 _pthread_wqthread + 758
2 libsystem_c.dylib 0x00007fff8b2acb85 start_wqthread + 13

Thread 4:
0 libsystem_kernel.dylib 0x00007fff8b5b5bca __psynch_cvwait + 10
1 libsystem_c.dylib 0x00007fff8b2ad274 _pthread_cond_wait + 840
2 liballegro-debug.5.0.dylib 0x000000010bbc3550 _al_cond_wait + 48 (aintuthr.h:81)
3 liballegro-debug.5.0.dylib 0x000000010bbbb51d al_wait_for_event + 253 (events.c:336)
4 furi_debug 0x000000010bb7f0a2 run_core + 82 (core.c:77)
5 furi_debug 0x000000010bb7edd5 run_once + 85 (engine.c:24)
6 furi_debug 0x000000010bb7ed59 _al_mangled_main + 25 (engine.c:34)
7 liballegro-debug.5.0.dylib 0x000000010bc7003f call_user_main + 31 (osx_app_delegate.m:214)
8 liballegro-debug.5.0.dylib 0x000000010bc70069 +[AllegroAppDelegate app_main:] + 25 (osx_app_delegate.m:226)
9 com.apple.Foundation 0x00007fff8ce6972a -[NSThread main] + 68
10 com.apple.Foundation 0x00007fff8ce696a2 _NSThread__main_ + 1575
11 libsystem_c.dylib 0x00007fff8b2a98bf _pthread_start + 335
12 libsystem_c.dylib 0x00007fff8b2acb75 thread_start + 13

Thread 5 Crashed:
0 liballegro-debug.5.0.dylib 0x000000010bbd31ad timer_thread_handle_tick + 77 (timernu.c:135)
1 liballegro-debug.5.0.dylib 0x000000010bbd3127 timer_thread_proc + 119 (timernu.c:111)
2 liballegro-debug.5.0.dylib 0x000000010bc71260 thread_proc_trampoline + 48 (uxthread.c:37)
3 libsystem_c.dylib 0x00007fff8b2a98bf _pthread_start + 335
4 libsystem_c.dylib 0x00007fff8b2acb75 thread_start + 13

Thread 5 crashed with X86 Thread State (64-bit):
rax: 0xffffffffffffffff rbx: 0x0000000000000000 rcx: 0x0000000000000000 rdx: 0x0000000000000008
rdi: 0x000000010bce51c0 rsi: 0x0000000000000000 rbp: 0x00000001103f5ea0 rsp: 0x00000001103f5e60
r8: 0x000000004fbfca9b r9: 0x0000000001f40000 r10: 0x0000000000000001 r11: 0x00007fff788a4228
r12: 0x0000000000007207 r13: 0x00000001103f6000 r14: 0x00007fe43190afd0 r15: 0x000000010bc71230
rip: 0x000000010bbd31ad rfl: 0x0000000000010206 cr2: 0x0000000000000097
Logical CPU: 2

I have also attached the full trace report. How do I fix this? Thanks.

Trent Gamblin
Member #261
April 2000
avatar

Hmm. Did you call al_init? al_create_timer? al_install_keyboard? Need more code.

iam_donald
Member #14,318
May 2012
avatar

Yes I did.

#SelectExpand
1static void init() { assert(al_init()); } 2 3ALLEGRO_EVENT_QUEUE * init_event_queue() 4 { 5 6 ALLEGRO_EVENT_QUEUE * queue = NULL; 7 queue = al_create_event_queue(); 8 if(not queue ) { perror_with_context("Failed to create ALLEGRO_EVENT_QUEUE", __LINE__, __FILE__); exit(EXIT_FAILURE); } 9 return queue; 10 } 11 12 ALLEGRO_TIMER * init_timer(ALLEGRO_EVENT_QUEUE * queue) 13 { 14 ALLEGRO_TIMER * timer = al_create_timer(ALLEGRO_BPS_TO_SECS(FPS)); 15 if(not timer) { perror_with_context("Failed to create ALLEGRO_TIMER", __LINE__, __FILE__); exit(EXIT_FAILURE); } 16 else { al_register_event_source(queue, al_get_timer_event_source(timer)); } 17 return timer; 18 } 19 20 ALLEGRO_EVENT_SOURCE * init_input(ALLEGRO_EVENT_QUEUE * queue) 21 { 22 ALLEGRO_EVENT_SOURCE * input; 23 if(not al_install_keyboard()) { perror_with_context("al_install_keyboard failed", __LINE__, __FILE__); exit(EXIT_FAILURE); } 24 else 25 { 26 input = al_get_keyboard_event_source(); 27 if(not input) { perror_with_context("Failed to create ALLEGRO_EVENT_SOURCE", __LINE__, __FILE__); exit(EXIT_FAILURE);} 28 else { al_register_event_source(queue, input); } 29 } 30 return input; 31 } 32 33void init_core(Core * core) 34 { 35 core->queue = init_event_queue(core->queue); 36 core->display = init_display(core->queue, W960_H540); 37 core->timer = init_timer(core->queue); 38 core->input = init_input(core ->queue); 39 /* init_font(); */ 40 /* init audio */ 41 /* init physics */ 42 }

I went ahead and debugged it in GDB, the output:

Starting program: /Volumes/Work/dev/furi/core/furi_debug
Reading symbols for shared libraries . done
[Switching to process 1265 thread 0x5603]

Breakpoint 1, al_wait_for_event (queue=0x101132900, ret_event=0x103125bc8) at events.c:330
330 ALLEGRO_EVENT *next_event = NULL;
(gdb) step
332 ASSERT(queue);
(gdb) step
334 _al_mutex_lock(&queue->mutex);
(gdb) print queue
$6 = (ALLEGRO_EVENT_QUEUE *) 0x101132900
(gdb) print *queue
$7 = {
sources = {
_itemsize = 8,
_items = 0x1011d7f20 "",
_size = 3,
_unused = 1
},
events = {
_itemsize = 72,
_items = 0x10111f310 "??\021\001\001",
_size = 1,
_unused = 0
},
events_head = 0,
events_tail = 0,
mutex = {
inited = true,
mutex = {
__sig = 1297437784,
__opaque = "\000\000\000\000` ", '\0' <repeats 11 times>, "\003\000\000\000\003\000\000\000\000\000\000\000\000\000\000h)\023\001\001\000\000\000l)\023\001\001\000\000\000\000\000\000\000\000\000\000"
}
},
cond = {
cond = {
__sig = 1129270852,
__opaque = "\000\000\000\000\000\000\000?", '\0' <repeats 12 times>, "\001", '\0' <repeats 18 times>
}
}
}
(gdb) step

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000097
[Switching to process 1265 thread 0x7307]
0x00000001000511ad in timer_thread_handle_tick (interval=70.631073999999998) at timernu.c:135
135 timer->counter -= interval;
(gdb)

I'm still unsure of what is causing the crash, the ALLEGRO_EVENT_QUEUE object passed is not NULL.

Peter Wang
Member #23
April 2000

How about the timer in the timer thread?

Trent Gamblin
Member #261
April 2000
avatar

I don't see anything there that would cause it. Can you upload a little project zip so I can try it out. Source code?

iam_donald
Member #14,318
May 2012
avatar

Peter wang: The timer in the timer thread is created by allegro when al_wait_for_event is called, the library takes care of initializing it I think.

Trent Gamblin: I've attached the source zip and a json file, the source loads images using the json file attached. Please edit the file with your own image file paths.

Besides Allegro, the other dependencies you be needing are tmalloc of gperftools (http://code.google.com/p/gperftools/) and jansson (http://www.digip.org/jansson/). The makefile uses clang as the compiler, gcc works fines as well. Make commands are 'make clean', 'make furi', 'make furi_debug'. Thanks for taking a look at this for me.

Peter Wang
Member #23
April 2000

Thread 5 Crashed:
0 liballegro-debug.5.0.dylib 0x000000010bbd31ad timer_thread_handle_tick + 77 (timernu.c:135)
1 liballegro-debug.5.0.dylib 0x000000010bbd3127 timer_thread_proc + 119 (timernu.c:111)
2 liballegro-debug.5.0.dylib 0x000000010bc71260 thread_proc_trampoline + 48 (uxthread.c:37)
3 libsystem_c.dylib 0x00007fff8b2a98bf _pthread_start + 335
4 libsystem_c.dylib 0x00007fff8b2acb75 thread_start + 13

This says the crash is in the timer thread. I think you are on the wrong path with al_wait_for_event.

Trent Gamblin
Member #261
April 2000
avatar

Yikes. I don't have time to install those deps and run and debug it right now. If you can break it down into a small test case then I can try that. Otherwise wait until tomorrow or something. But what Peter said is true.

iam_donald
Member #14,318
May 2012
avatar

Fixed it. Thanks for the guidance.

Peter Wang
Member #23
April 2000

It was a bug in your program, correct? Allegro bugs keep me up at night.

iam_donald
Member #14,318
May 2012
avatar

Yeah I botched a couple of mallocs in my code, silly me . You guys [contributors] are doing a fine job with Allegro. It's really powerful. Your work is very much appreciated :)

Go to: