diff --git a/darwin/alloc.m b/darwin/alloc.m index 0ce46404..887d658d 100644 --- a/darwin/alloc.m +++ b/darwin/alloc.m @@ -23,13 +23,10 @@ void initAlloc(void) void uninitAlloc(void) { NSMutableString *str; - NSUInteger i; NSValue *v; // delegates might have mapTables allocated // TODO verify they are empty - for (i = 0; i < [delegates count]; i++) - [[delegates objectAtIndex:i] release]; [delegates release]; if ([allocations count] == 0) { [allocations release]; diff --git a/darwin/button.m b/darwin/button.m index be45dba9..baccabbb 100644 --- a/darwin/button.m +++ b/darwin/button.m @@ -103,7 +103,7 @@ uiButton *uiNewButton(const char *text) uiDarwinSetControlFont(b->button, NSRegularControlSize); if (buttonDelegate == nil) { - buttonDelegate = [buttonDelegateClass new]; + buttonDelegate = [[buttonDelegateClass new] autorelease]; [delegates addObject:buttonDelegate]; } [buttonDelegate registerButton:b]; diff --git a/darwin/checkbox.m b/darwin/checkbox.m index 3914cfd4..cdacf419 100644 --- a/darwin/checkbox.m +++ b/darwin/checkbox.m @@ -118,7 +118,7 @@ uiCheckbox *uiNewCheckbox(const char *text) uiDarwinSetControlFont(c->button, NSRegularControlSize); if (checkboxDelegate == nil) { - checkboxDelegate = [checkboxDelegateClass new]; + checkboxDelegate = [[checkboxDelegateClass new] autorelease]; [delegates addObject:checkboxDelegate]; } [checkboxDelegate registerCheckbox:c]; diff --git a/darwin/combobox.m b/darwin/combobox.m index 7e047e9c..c1bffdce 100644 --- a/darwin/combobox.m +++ b/darwin/combobox.m @@ -135,7 +135,7 @@ uiCombobox *uiNewCombobox(void) options:nil]; if (comboboxDelegate == nil) { - comboboxDelegate = [comboboxDelegateClass new]; + comboboxDelegate = [[comboboxDelegateClass new] autorelease]; [delegates addObject:comboboxDelegate]; } [comboboxDelegate registerCombobox:c]; diff --git a/darwin/editablecombo.m b/darwin/editablecombo.m index 2387f0fe..67259521 100644 --- a/darwin/editablecombo.m +++ b/darwin/editablecombo.m @@ -175,7 +175,7 @@ uiEditableCombobox *uiNewEditableCombobox(void) uiDarwinSetControlFont(c->cb, NSRegularControlSize); if (comboboxDelegate == nil) { - comboboxDelegate = [editableComboboxDelegateClass new]; + comboboxDelegate = [[editableComboboxDelegateClass new] autorelease]; [delegates addObject:comboboxDelegate]; } [comboboxDelegate registerCombobox:c]; diff --git a/darwin/entry.m b/darwin/entry.m index 43f450d4..179c81e1 100644 --- a/darwin/entry.m +++ b/darwin/entry.m @@ -158,7 +158,7 @@ uiEntry *uiNewEntry(void) e->textfield = newEditableTextField(); if (entryDelegate == nil) { - entryDelegate = [entryDelegateClass new]; + entryDelegate = [[entryDelegateClass new] autorelease]; [delegates addObject:entryDelegate]; } [entryDelegate registerEntry:e]; diff --git a/darwin/main.m b/darwin/main.m index c34ede14..dd7f2d60 100644 --- a/darwin/main.m +++ b/darwin/main.m @@ -3,6 +3,8 @@ static BOOL canQuit = NO; static NSAutoreleasePool *globalPool; +static applicationClass* app; +static appDelegate* delegate; @implementation applicationClass @@ -101,15 +103,14 @@ uiInitOptions options; const char *uiInit(uiInitOptions *o) { - globalPool = [[NSAutoreleasePool alloc] init]; - @autoreleasepool { options = *o; - [applicationClass sharedApplication]; + app = [[applicationClass sharedApplication] retain]; // don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy! // see https://github.com/andlabs/ui/issues/6 [realNSApp() setActivationPolicy:NSApplicationActivationPolicyRegular]; - [realNSApp() setDelegate:[appDelegate new]]; + delegate = [appDelegate new]; + [realNSApp() setDelegate:delegate]; initAlloc(); @@ -118,20 +119,26 @@ const char *uiInit(uiInitOptions *o) [realNSApp() setMainMenu:[appDelegate().menuManager makeMenubar]]; setupFontPanel(); - - return NULL; } + + globalPool = [[NSAutoreleasePool alloc] init]; + + return NULL; } void uiUninit(void) { - @autoreleasepool { - [appDelegate() release]; - [realNSApp() setDelegate:nil]; - [realNSApp() release]; - uninitAlloc(); + if (!globalPool) { + userbug("You must call uiInit() first!"); } [globalPool release]; + + @autoreleasepool { + [delegate release]; + [realNSApp() setDelegate:nil]; + [app release]; + uninitAlloc(); + } } void uiFreeInitError(const char *err) diff --git a/darwin/slider.m b/darwin/slider.m index 4cee88c5..53274574 100644 --- a/darwin/slider.m +++ b/darwin/slider.m @@ -138,7 +138,7 @@ uiSlider *uiNewSlider(intmax_t min, intmax_t max) [cell setSliderType:NSLinearSlider]; if (sliderDelegate == nil) { - sliderDelegate = [sliderDelegateClass new]; + sliderDelegate = [[sliderDelegateClass new] autorelease]; [delegates addObject:sliderDelegate]; } [sliderDelegate registerSlider:s]; diff --git a/darwin/tab.m b/darwin/tab.m index 0e7a0d19..24060309 100644 --- a/darwin/tab.m +++ b/darwin/tab.m @@ -35,8 +35,8 @@ struct uiTab { { self = [super init]; if (self != nil) { - self->view = v; - self->pageID = o; + self->view = [v retain]; + self->pageID = [o retain]; } return self; } @@ -189,14 +189,14 @@ void uiTabInsertAt(uiTab *t, const char *name, uintmax_t n, uiControl *child) uiControlSetParent(child, uiControl(t)); - view = [[NSView alloc] initWithFrame:NSZeroRect]; + view = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease]; // TODO if we turn off the autoresizing mask, nothing shows up; didn't this get documented somewhere? uiDarwinControlSetSuperview(uiDarwinControl(child), view); uiDarwinControlSyncEnableState(uiDarwinControl(child), uiControlEnabledToUser(uiControl(t))); // the documentation says these can be nil but the headers say these must not be; let's be safe and make them non-nil anyway pageID = [NSObject new]; - page = [[tabPage alloc] initWithView:view pageID:pageID]; + page = [[[tabPage alloc] initWithView:view pageID:pageID] autorelease]; page.c = child; // don't hug, just in case we're a stretchy tab @@ -206,16 +206,11 @@ void uiTabInsertAt(uiTab *t, const char *name, uintmax_t n, uiControl *child) uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationVertical); [t->pages insertObject:page atIndex:n]; - [page release]; // no need for initial reference - i = [[NSTabViewItem alloc] initWithIdentifier:pageID]; + i = [[[NSTabViewItem alloc] initWithIdentifier:pageID] autorelease]; [i setLabel:toNSString(name)]; [i setView:view]; [t->tabview insertTabViewItem:i atIndex:n]; - // TODO release i? - - [pageID release]; // no need for initial reference - [view release]; tabRelayout(t); } diff --git a/darwin/window.m b/darwin/window.m index 58bd63ac..e5ebc93f 100644 --- a/darwin/window.m +++ b/darwin/window.m @@ -258,7 +258,7 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) [w->window setReleasedWhenClosed:NO]; if (windowDelegate == nil) { - windowDelegate = [windowDelegateClass new]; + windowDelegate = [[windowDelegateClass new] autorelease]; [delegates addObject:windowDelegate]; } [windowDelegate registerWindow:w];