diff --git a/new/alloc_darwin.m b/new/alloc_darwin.m index c347e7a..8f539af 100644 --- a/new/alloc_darwin.m +++ b/new/alloc_darwin.m @@ -7,7 +7,7 @@ void *uiAlloc(size_t size, const char *type) void *out; out = malloc(size); - if (out != NULL) { + if (out == NULL) { fprintf(stderr, "memory exhausted in uiAlloc() allocating %s\n", type); abort(); } @@ -24,7 +24,7 @@ void *uiRealloc(void *p, size_t size, const char *type) if (p == NULL) return uiAlloc(size, type); out = realloc(p, size); - if (out != NULL) { + if (out == NULL) { fprintf(stderr, "memory exhausted in uiRealloc() reallocating %s\n", type); abort(); } diff --git a/new/button_darwin.m b/new/button_darwin.m index a4191a9..92e2c76 100644 --- a/new/button_darwin.m +++ b/new/button_darwin.m @@ -58,7 +58,7 @@ char *uiButtonText(uiControl *c) uiNSButton *b; b = (uiNSButton *) uiControlHandle(c); - return strdup(fromNSString([b title])); + return uiDarwinNSStringToText([b title]); } void uiButtonSetText(uiControl *c, const char *text) diff --git a/new/checkbox_darwin.m b/new/checkbox_darwin.m index a07a4d0..ca52389 100644 --- a/new/checkbox_darwin.m +++ b/new/checkbox_darwin.m @@ -57,7 +57,7 @@ char *uiCheckboxText(uiControl *c) uiCheckboxNSButton *cc; cc = (uiCheckboxNSButton *) uiControlHandle(c); - return strdup(fromNSString([cc title])); + return uiDarwinNSStringToText([cc title]); } void uiCheckboxSetText(uiControl *c, const char *text) diff --git a/new/entry_darwin.m b/new/entry_darwin.m index 4a4047a..102168b 100644 --- a/new/entry_darwin.m +++ b/new/entry_darwin.m @@ -55,8 +55,7 @@ char *uiEntryText(uiControl *c) uiNSTextField *t; t = (uiNSTextField *) uiControlHandle(c); - // TODO wrap all strdup calls - return strdup(fromNSString([t stringValue])); + return uiDarwinNSStringToText([t stringValue]); } void uiEntrySetText(uiControl *c, const char *text) diff --git a/new/text_darwin.m b/new/text_darwin.m new file mode 100644 index 0000000..4f36356 --- /dev/null +++ b/new/text_darwin.m @@ -0,0 +1,14 @@ +// 10 april 2015 +#import "uipriv_darwin.h" + +char *uiDarwinNSStringToText(NSString *s) +{ + char *out; + + out = strdup([s UTF8String]); + if (out == NULL) { + fprintf(stderr, "memory exhausted in uiDarwinNSStringToText()\n"); + abort(); + } + return out; +} diff --git a/new/ui_darwin.h b/new/ui_darwin.h index 672eb63..153ff51 100644 --- a/new/ui_darwin.h +++ b/new/ui_darwin.h @@ -15,6 +15,9 @@ This file assumes that you have imported and "ui.h" beforehand. extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder); extern BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview); +// You can use this function from within your control implementations to return text strings that can be freed with uiTextFree(). +extern char *uiDarwinNSStringToText(NSString *); + struct uiSizingSys { // this structure currently left blank }; diff --git a/new/window_darwin.m b/new/window_darwin.m index 0530386..690c73a 100644 --- a/new/window_darwin.m +++ b/new/window_darwin.m @@ -81,7 +81,7 @@ uintptr_t uiWindowHandle(uiWindow *w) char *uiWindowTitle(uiWindow *w) { - return strdup(fromNSString([D.w title])); + return uiDarwinNSStringToText([D.w title]); } void uiWindowSetTitle(uiWindow *w, const char *title)