From: Mikko Rasa Date: Thu, 17 Oct 2013 07:38:15 +0000 (+0300) Subject: Tighten memory management in ObjC code X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ed2ada6539882e02e1054a3394bb62a182bf007b;p=libs%2Fgui.git Tighten memory management in ObjC code --- diff --git a/source/graphics/cocoa/cocoadisplay.m b/source/graphics/cocoa/cocoadisplay.m index 3445c17..9c7711e 100644 --- a/source/graphics/cocoa/cocoadisplay.m +++ b/source/graphics/cocoa/cocoadisplay.m @@ -1,4 +1,5 @@ #import +#import #import #include "cocoadisplay.h" @@ -7,6 +8,7 @@ void convert_event(NSEvent *, CocoaEvent *); struct _CocoaDisplay { NSApplication *app; + NSAutoreleasePool *arp; CFMutableArrayRef event_queue; }; @@ -16,6 +18,7 @@ void free_event(CFAllocatorRef, const void *); CocoaDisplay *create_display() { CocoaDisplay *display = (CocoaDisplay *)malloc(sizeof(CocoaDisplay)); + display->arp = [[NSAutoreleasePool alloc] init]; display->app = [NSApplication sharedApplication]; // This makes windows appear when run from terminal if([display->app respondsToSelector:@selector(setActivationPolicy:)]) @@ -36,6 +39,7 @@ CocoaDisplay *create_display() void destroy_display(CocoaDisplay *display) { CFRelease(display->event_queue); + [display->arp release]; free(display); } @@ -56,8 +60,10 @@ bool get_event(CocoaDisplay *display, CocoaEvent *buf) NSEvent *event = [display->app nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES]; if(event) { + [event retain]; [display->app sendEvent:event]; convert_event(event, buf); + [event release]; return true; } diff --git a/source/graphics/cocoa/cocoawindow.m b/source/graphics/cocoa/cocoawindow.m index 6929520..9ed5133 100644 --- a/source/graphics/cocoa/cocoawindow.m +++ b/source/graphics/cocoa/cocoawindow.m @@ -13,6 +13,7 @@ struct _CocoaWindow { CocoaDisplay *display; __strong NSWindow *window; + __strong WindowDelegate *delegate; NSUInteger style; }; @@ -36,7 +37,8 @@ CocoaWindow *create_window(CocoaDisplay *display, unsigned width, unsigned heigh wrapper->window = window; wrapper->style = style; - [window setDelegate:[[WindowDelegate alloc] init]]; + wrapper->delegate = [[WindowDelegate alloc] init]; + [window setDelegate:wrapper->delegate]; if(!windows) windows = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); @@ -55,6 +57,7 @@ void destroy_window(CocoaWindow *window) } [window->window release]; + [window->delegate release]; free(window); }