Objective-C: compiler optimized variable

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 viewing variables in gdb after line 1 execution

 p checkLicenseURL 

returns

 $1 = <variable optimized away by compiler> 

This also causes line 3 to crash. Why is this happening and how to fix it?

+4
source share
2 answers

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

 -O0 -fno-inline 
+8
source

Turning off optimization for everything is one option. It is also possible to instruct the compiler that specific variables should not be optimized. A way to do this with the volatile keyword:

 volatile NSURL *checkLicenseURL = ... 

Wikipedia entry on mutable variables

Another similar question: Compiler optimized iPhone variable

+5
source

All Articles