Title: Objective-C: variable optimized away by compiler
Tags: objective-c;cocoa;xcode;compiler-construction;gdb
Question: I am trying to run the following code:

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

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

```p checkLicenseURL
```

returns

```$1 = <variable optimized away by compiler&gt;
```

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


Here is the accepted 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
```

Comment for this answer: I'm using the "debug" mode in Xcode, but it is still giving this error.

Here is another answer: 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


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:12/28/09', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<objective-c><cocoa><xcode><compiler-construction><gdb>', 'Title': 'Objective-C: variable optimized away by compiler', 'ViewCount': 2420, 'id': 1968040, 'user_AboutMe': '<p>Generalist software engineer with 10 years of experience shipping products featured by Apple and Google, ranging from full-stack mobile and web development, game development, UI / UX design, and AI / machine learning.</p>\n', 'user_AccountId': 85365.0, 'user_DisplayName': 'Chetan', 'user_Reputation': 44962.0, 'user_Views': 1661.0, 'user_id': 238030.0}}