From 08c05414253818b30b40fe8f5202c56237fed00d Mon Sep 17 00:00:00 2001
From: BQ <chinbillybilbo@gmail.com>
Date: Fri, 25 Jan 2013 20:18:40 +0000
Subject: [PATCH] Fix for iOS crash. Display is stored in TLS. We cannot get
 the view on a thread that did not create the display. Must
 pass to main thread when creating the alert view.

---
 addons/native_dialog/iphone_dialog.m | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/addons/native_dialog/iphone_dialog.m b/addons/native_dialog/iphone_dialog.m
index f4a7929..6dd3762 100644
--- a/addons/native_dialog/iphone_dialog.m
+++ b/addons/native_dialog/iphone_dialog.m
@@ -41,8 +41,10 @@ bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
     al_signal_cond(button_pressed);
     al_unlock_mutex(mutex);
 }
--(void) createAlert:(id)alert {
-    [al_iphone_get_view(al_get_current_display()) addSubview:alert];
+- (void) createAlert:(NSArray*)array {
+    UIView *view = [array objectAtIndex:0];
+    UIAlertView *alert = [array objectAtIndex:1];
+    [view addSubview:alert];
     [alert show];
     [alert release];
 }
@@ -63,6 +65,9 @@ int _al_show_native_message_box(ALLEGRO_DISPLAY *display,
     AlertDelegate *delegate = [[AlertDelegate alloc]init];
     delegate.mutex = al_create_mutex();
     delegate.button_pressed = al_create_cond();
+    
+    // This needs to be done on the thread with the display due to TLS.
+    UIView *view = al_iphone_get_view(al_get_current_display());
 
     UIAlertView *alert = [[UIAlertView alloc]
                           initWithTitle:title
@@ -72,9 +77,8 @@ int _al_show_native_message_box(ALLEGRO_DISPLAY *display,
                           otherButtonTitles:nil];
 
     [delegate performSelectorOnMainThread:@selector(createAlert:)
-                               withObject:alert
-                            waitUntilDone:YES];
-    
+                               withObject:@[view,alert]
+                            waitUntilDone:YES];    
 
     al_lock_mutex(delegate.mutex);
     al_wait_cond(delegate.button_pressed, delegate.mutex);
-- 
1.7.11.1

