Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: variable optimized away by compiler

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?

like image 959
Chetan Avatar asked Dec 13 '22 00:12

Chetan


2 Answers

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
like image 80
Dan Olson Avatar answered Dec 15 '22 13:12

Dan Olson


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

like image 39
Evan Avatar answered Dec 15 '22 14:12

Evan