2

I have the following objective-c snippet in my hello world example:

//hello.m

#import <Foundation/Foundation.h>
#import "hello.h"

void sayHello()
{
    #ifdef FRENCH
    NSString *helloWorld = @"Bonjour Monde!\n";
    #else
    NSString *helloWorld = @"Hello World\n";
    #endif
    printf("%s", [helloWorld UTF8String]);
}


//main.m
#import <Foundation/Foundation.h>
#import "hello.h"

int main (int argc, const char * argv[])
{
    sayHello();
    return 0;
}

building this stuff on osx works fine and runs as expected. But when compiling/linking it on ubuntu (using GNUStep) results in an segmentation fault when executing the binary. I nailed it down to the casting operation in the printf statement, but I have no clue what I'm doing wrong here or how I can solve this.

Interesting note: This works fine when using gcc toolchain to build the executable. I just see this issue when building it with clang on ubuntu.

Any help is very much appreciated.

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • by casting I meant this particular statement: "[helloWorld UTF8String]" – Rene Groeschke Jan 20 '14 at 19:38
  • `[helloWorld UTF8String]` is a method call, not a cast. Please provide the rest of your code for context. What you have posted won't compile (no functions/methods, etc) – BergQuester Jan 20 '14 at 19:49
  • 1
    UTF8String returns a `const char*` version of the NSString. It's existence is tied to the lifetime of the NSString object in a manner that could be easily mucked up if the storage management environment is even slightly corrupt. – Hot Licks Jan 20 '14 at 19:58
  • The snippet above is all the code I'm trying to execute. So directly after setting up the NSString, I call the printf statement. So from my humble point of view nothing should mucked up... – Rene Groeschke Jan 20 '14 at 20:06
  • ObjC, like C, requires a `main()` function as its entry point. I'd think your code would fail to compile without that, rather than segfault, but I suppose it's possible that gcc compiles something which then falls over at runtime. Try putting this inside a `main()`. – jscs Jan 20 '14 at 20:53
  • I've updated my snippet. I have this code in a method which is called in a main() method. Compilation and linking works fine. just running results in the segmentation fault. using gdb I traced it down to *[helloWorld UTF8String]* statement – Rene Groeschke Jan 20 '14 at 21:05
  • 1
    have you been through this tutorial for GNUstep? http://www.gnustep.it/nicola/Tutorials/WritingMakefiles/node6.html. Curious are you launching it using openapp? This doesn't seem like a code issue with your hello.m file to me. – foggzilla Jan 21 '14 at 16:33
  • thanks foggzilla. I've seen this tutorial, but I built an executable and not a whole app. By executing my executable as root, everything works fine, just as expected. Interesting note: It also workds when using gcc toolchain. I just see this issue when using clang on ubuntu. – Rene Groeschke Jan 21 '14 at 17:26

1 Answers1

1

To fix this issue, I ended up changing my code to the following:

...
void sayHello()
{
    #ifdef FRENCH
    NSString *helloWorld = @"${HELLO_WORLD_FRENCH}\\n";
    #else
    NSString *helloWorld = @"${HELLO_WORLD}\\n";
    #endif

    NSFileHandle *stdout = [NSFileHandle fileHandleWithStandardOutput];
    NSData *strData = [helloWorld dataUsingEncoding: NSASCIIStringEncoding];
    [stdout writeData: strData];
}
...
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78