diff --git a/alloc_darwin.m b/alloc_darwin.m index c347e7a1..8f539af7 100644 --- a/alloc_darwin.m +++ b/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/button_darwin.m b/button_darwin.m index a4191a9f..92e2c76e 100644 --- a/button_darwin.m +++ b/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/checkbox_darwin.m b/checkbox_darwin.m index a07a4d0e..ca52389c 100644 --- a/checkbox_darwin.m +++ b/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/entry_darwin.m b/entry_darwin.m index 4a4047a0..102168be 100644 --- a/entry_darwin.m +++ b/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/text_darwin.m b/text_darwin.m new file mode 100644 index 00000000..4f363561 --- /dev/null +++ b/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/ui_darwin.h b/ui_darwin.h index 672eb631..153ff517 100644 --- a/ui_darwin.h +++ b/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/window_darwin.m b/window_darwin.m index 05303869..690c73ac 100644 --- a/window_darwin.m +++ b/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)