R – Objective-C: variable optimized away by compiler

cocoacompiler-constructiongdbobjective cxcode

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?

Best Answer

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