From 310d9a8cfe1a7cc4240fe124fffe87bb4ff32cfb Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Tue, 12 Feb 2008 12:33:55 +0100 Subject: Objective-C layer: Make initialisation more robust. darcs-hash:27c95a5ba79bdd0efc3d18c0ee4caa83d7264394 --- Objective-C/libobjcl.m | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'Objective-C') diff --git a/Objective-C/libobjcl.m b/Objective-C/libobjcl.m index 92fc3cf..b380b6e 100644 --- a/Objective-C/libobjcl.m +++ b/Objective-C/libobjcl.m @@ -54,34 +54,32 @@ void *objcl_current_exception_lock = NULL; static NSMutableDictionary *method_lists = NULL; static NSMutableDictionary *method_list_lengths = NULL; +static int init_count = 0; + void objcl_initialise_runtime (void) { - if (!objcl_autorelease_pool) - objcl_autorelease_pool = [[NSAutoreleasePool alloc] init]; - - if (!objcl_oom_exception) + if (init_count <= 0) { + objcl_autorelease_pool = [[NSAutoreleasePool alloc] init]; objcl_oom_exception = [NSException exceptionWithName: @"MLKOutOfMemoryException" reason: @"Out of memory" userInfo: nil]; [objcl_oom_exception retain]; - } #ifdef __NEXT_RUNTIME__ - PyObjC_SetupRuntimeCompat (); + PyObjC_SetupRuntimeCompat (); #endif - objcl_initialise_lock (&objcl_current_exception_lock); - - if (!method_lists) - method_lists = [[NSMutableDictionary alloc] init]; - - if (!method_list_lengths) - method_list_lengths = [[NSMutableDictionary alloc] init]; - - objcl_initialise_instance_wrappers (); + objcl_initialise_lock (&objcl_current_exception_lock); + method_lists = [[NSMutableDictionary alloc] init]; + method_list_lengths = [[NSMutableDictionary alloc] init]; + objcl_initialise_instance_wrappers (); + init_count = 1; + } + else + init_count++; } @@ -99,11 +97,17 @@ release_unless_null (id *object) void objcl_shutdown_runtime (void) { - release_unless_null (&objcl_autorelease_pool); - release_unless_null (&objcl_oom_exception); - release_unless_null (&method_lists); - release_unless_null (&method_list_lengths); - objcl_shutdown_instance_wrappers (); + init_count--; + if (init_count == 0) + { + release_unless_null (&objcl_autorelease_pool); + release_unless_null (&objcl_oom_exception); + release_unless_null (&method_lists); + release_unless_null (&method_list_lengths); + objcl_shutdown_instance_wrappers (); + } + else if (init_count < 0) + init_count = 0; } -- cgit v1.2.3