views:

281

answers:

2

I am trying to run the following code:

1. NSURL *checkLicenseURL = [NSURL URLWithString:@"check_license.php?accesskey=&license_key="];
// call server API
2. NSError *err = nil;
3. NSXMLDocument *xmlResult = [[NSXMLDocument alloc] initWithContentsOfURL:checkLicenseURL options:NSXMLDocumentTidyXML error:&err];

But when looking at variables in gdb, after line 1 was executed, doing

p checkLicenseURL

returns

$1 = <variable optimized away by compiler>

It also causes line 3 to crash. Why is this happening and how do I fix this?

+7  A: 

Just compile without optimizations turned on, or select a "debug" build if you used a wizard of some sort to build your project. I'm not sure where to turn off optimizations in XCode but you probably want these GCC command line options for debugging:

-O0 -fno-inline
Dan Olson
I'm using the "debug" mode in Xcode, but it is still giving this error.
Chetan
Never mind, this was the correct answer.
Chetan
+1  A: 

Turning off optimizations for everything is one option. It is also possible to instruct the compiler that particular variables should not be optimized away. The way to do it is with the volatile keyword:

volatile NSURL *checkLicenseURL = ...

Wikipedia entry on volatile variables

Another similar question: iPhone Variable Optimized Away by Compiler

Evan