]> git.tdb.fi Git - libs/gui.git/commitdiff
Tighten memory management in ObjC code
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Oct 2013 07:38:15 +0000 (10:38 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Oct 2013 07:38:15 +0000 (10:38 +0300)
source/graphics/cocoa/cocoadisplay.m
source/graphics/cocoa/cocoawindow.m

index 3445c176cad0911337030543c1e52ae30f441c36..9c7711e01d2c551323d9d7cb879e27613ed4d67c 100644 (file)
@@ -1,4 +1,5 @@
 #import <AppKit/NSApplication.h>
+#import <Foundation/NSAutoReleasePool.h>
 #import <Foundation/NSRunLoop.h>
 #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;
        }
 
index 692952002f7c6be6f5d60be10266ba2d5e03b58a..9ed5133162d3c6ff791a62521b67624f38eb3550 100644 (file)
@@ -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);
 }