@@ -53,18 +53,18 @@ id delegate = [app delegate]; if ([delegate respondsToSelector: @selector(applicationWillTerminate)]) [delegate applicationWillTerminate]; - [(id)delegate release]; + [delegate release]; } #define SIGNAL_HANDLER(sig) \ static void \ handle##sig(int signal) \ { \ - app->sig##Handler(app->delegate, \ + app->_##sig##Handler(app->_delegate, \ @selector(applicationDidReceive##sig)); \ } SIGNAL_HANDLER(SIGINT) #ifndef _WIN32 SIGNAL_HANDLER(SIGHUP) @@ -137,10 +137,11 @@ { self = [super init]; @try { void *pool; + OFMutableDictionary *environment; #if defined(__MACH__) && !defined(OF_IOS) char **env = *_NSGetEnviron(); #elif defined(__WIN32) uint16_t *env; #elif !defined(OF_IOS) @@ -267,29 +268,39 @@ objc_autoreleasePoolPop(pool); #endif [environment makeImmutable]; + _environment = environment; } @catch (id e) { [self release]; @throw e; } return self; } -- (void)OF_setArgumentCount: (int*)argc_ - andArgumentValues: (char***)argv_ +- (void)dealloc +{ + [_arguments release]; + [_environment release]; + + [super dealloc]; +} + +- (void)OF_setArgumentCount: (int*)argc + andArgumentValues: (char***)argv { #ifndef _WIN32 void *pool = objc_autoreleasePoolPush(); + OFMutableArray *arguments; int i; - argc = argc_; - argv = argv_; + _argc = argc; + _argv = argv; - programName = [[OFString alloc] + _programName = [[OFString alloc] initWithCString: (*argv)[0] encoding: OF_STRING_ENCODING_NATIVE]; arguments = [[OFMutableArray alloc] init]; for (i = 1; i < *argc; i++) @@ -296,73 +307,76 @@ [arguments addObject: [OFString stringWithCString: (*argv)[i] encoding: OF_STRING_ENCODING_NATIVE]]; [arguments makeImmutable]; - - objc_autoreleasePoolPop(pool); -#else - argc = argc_; - argv = argv_; -#endif -} - -#ifdef _WIN32 -- (void)OF_setArgumentCount: (int)argc_ - andWideArgumentValues: (wchar_t**)argv_ -{ - void *pool = objc_autoreleasePoolPush(); - int i; - - programName = [[OFString alloc] initWithUTF16String: argv_[0]]; - arguments = [[OFMutableArray alloc] init]; - - for (i = 1; i < argc_; i++) - [arguments addObject: - [OFString stringWithUTF16String: argv_[i]]]; - - [arguments makeImmutable]; - - objc_autoreleasePoolPop(pool); -} -#endif - -- (void)getArgumentCount: (int**)argc_ - andArgumentValues: (char****)argv_ -{ - *argc_ = argc; - *argv_ = argv; -} - -- (OFString*)programName -{ - OF_GETTER(programName, NO) -} - -- (OFArray*)arguments -{ - OF_GETTER(arguments, NO) -} - -- (OFDictionary*)environment -{ - OF_GETTER(environment, NO) -} - -- (id )delegate -{ - return delegate; -} - -- (void)setDelegate: (id )delegate_ -{ - delegate = delegate_; - -#define REGISTER_SIGNAL(sig) \ - if ([delegate respondsToSelector: \ - @selector(applicationDidReceive##sig)]) { \ - sig##Handler = (void(*)(id, SEL))[(id)delegate \ + _arguments = arguments; + + objc_autoreleasePoolPop(pool); +#else + _argc = argc; + _argv = argv; +#endif +} + +#ifdef _WIN32 +- (void)OF_setArgumentCount: (int)argc + andWideArgumentValues: (wchar_t**)argv +{ + void *pool = objc_autoreleasePoolPush(); + OFMutableArray *arguments; + int i; + + _programName = [[OFString alloc] initWithUTF16String: argv[0]]; + arguments = [[OFMutableArray alloc] init]; + + for (i = 1; i < argc; i++) + [arguments addObject: + [OFString stringWithUTF16String: argv[i]]]; + + [arguments makeImmutable]; + _arguments = arguments; + + objc_autoreleasePoolPop(pool); +} +#endif + +- (void)getArgumentCount: (int**)argc + andArgumentValues: (char****)argv +{ + *argc = _argc; + *argv = _argv; +} + +- (OFString*)programName +{ + OF_GETTER(_programName, NO) +} + +- (OFArray*)arguments +{ + OF_GETTER(_arguments, NO) +} + +- (OFDictionary*)environment +{ + OF_GETTER(_environment, NO) +} + +- (id )delegate +{ + return _delegate; +} + +- (void)setDelegate: (id )delegate +{ + _delegate = delegate; + +#define REGISTER_SIGNAL(sig) \ + if ([delegate respondsToSelector: \ + @selector(applicationDidReceive##sig)]) { \ + _##sig##Handler = (void(*)(id, SEL))[(id)delegate \ methodForSelector: \ @selector(applicationDidReceive##sig)]; \ signal(sig, handle##sig); \ } else \ signal(sig, SIG_DFL); @@ -389,12 +403,18 @@ [OFRunLoop OF_setMainRunLoop: runLoop]; objc_autoreleasePoolPop(pool); + /* + * Note: runLoop is still valid after the release of the pool, as + * OF_setMainRunLoop: retained it. However, we only have a weak + * reference to it now, whereas we had a strong reference before. + */ + pool = objc_autoreleasePoolPush(); - [delegate applicationDidFinishLaunching]; + [_delegate applicationDidFinishLaunching]; objc_autoreleasePoolPop(pool); [runLoop run]; } @@ -405,14 +425,6 @@ - (void)terminateWithStatus: (int)status { exit(status); } - -- (void)dealloc -{ - [arguments release]; - [environment release]; - - [super dealloc]; -} @end