Segmentation fault when attemping to print NSString as UTF8String
Asked Answered
N

1

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.

Nalley answered 20/1, 2014 at 19:30 Comment(8)
by casting I meant this particular statement: "[helloWorld UTF8String]"Nalley
[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)Woodson
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.Charterhouse
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...Nalley
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().Wristwatch
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] statementNalley
have you been through this tutorial for GNUstep? 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.Scrofula
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.Nalley
N
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];
}
...
Nalley answered 23/1, 2014 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.