42

I have upgraded to Xcode 5.0. And when I run an app in debug mode and try to print an NSString value in console, it gives me the below error. Any ideas?

error: warning: couldn't get cmd pointer (substituting NULL): Couldn't load '_cmd' because its value couldn't be evaluated
Couldn't materialize struct: the variable 'stringValue' has no location, it may have been optimized out
Errored out in Execute, couldn't PrepareToExecuteJITExpression

Here is the code:

NSString *stringValue = [[self.responseArray objectAtIndex:i] valueForKey:@"merchant_name"]; 
6
  • 3
    Can you show the code you are debugging?
    – bneely
    Oct 1, 2013 at 8:12
  • NSString *stringValue = [[self.responseArray objectAtIndex:i] valueForKey:@"merchant_name"]; when i tried to print po stringValue it gives me the above error
    – Arun
    Oct 1, 2013 at 8:15
  • What is "i" counter? Are you sure you have "merchant_name" in your dic? Try some debugging.
    – Jakub
    Oct 1, 2013 at 8:16
  • yes i did try and NSString has got the value. its just not displaying the value in console
    – Arun
    Oct 1, 2013 at 8:18
  • 1
    use objectForKey instead of valueForKey, could you give us : NSLog(@"%@", _responseArray); please ? Oct 1, 2013 at 8:29

5 Answers 5

81

The reason is stated in the error message: it may have been optimized out.. this means that you are compiling and running your code in an optimized manner.

you gotta change your compiler optimization level from Fastest,Smallest to none:

  • go to your target build settings
  • search for optimization level
  • change it to none (whatever mode you are in ie debugging, distribution or release)
  • profit

do the same for your project settings

4
  • 8
    I'm suffering that same issue. Changing compiler optimization level won't work for me. The problem only appears while trying to debug code that belongs to a library my app uses (The library is included into my project and compiled by myself). I changed the optimizations settings in the library project, library target, main app project and main app target with no luck.
    – The dude
    Nov 7, 2013 at 11:34
  • There are many ways this can occur. For me it was because the "Other C Flags" value was set to "-O2", even for the debug build. Turning this off for the debug build resolved the issue.
    – ThomasW
    Jun 17, 2014 at 10:38
  • @abbood thanks.. its help me a lot, I just want to know to changing optimisation level to none will it effect on performance.??
    – Tapas Pal
    Jul 24, 2014 at 5:39
  • 2
    @TapasPal naturally it would.. you want fullest optimization level for your production scheme
    – abbood
    Jul 24, 2014 at 5:41
38

Make sure you are in debug mode. Go Edit Scheme > Build Configuration > Debug

2
  • The problem is it does print NsArray and other values. only in this case it gives that error. Moreover i am getting this error after upgrading to Xcode5. Looks like its a compiler error.
    – Arun
    Oct 1, 2013 at 8:23
  • @Arun it's not a compiler error, it's just a compiler setting.. see my answer
    – abbood
    Oct 1, 2013 at 8:56
9

You might be trying to debug in the "release Scheme". Go to "Product/Scheme/Edit Scheme" and pick the "run" in the left panel. Then change the build configuration to "debug".

4

One alternate answer: instead of fixing "it may have been optimized out" by removing the optimization, you can stop it from being optimized by using the variable.

So, in your question, if you do something with stringValue:

NSString *stringValue = [[self.responseArray objectAtIndex:i] valueForKey:@"merchant_name"]; 
NSLog(@"%@", stringValue);

stringValue will no longer be optimized out because you're using it.

If you want to see all instances of optimized-out variables in your project, Use Product -> Analyze to analyze your project. Any "Dead store" warnings (in which a value is stored, and never read) will be optimized out at compile time if you have compiler optimization turned on.

-1

This error seems to be showing for me despite being in debug mode with 0 optimization level. Seems to be an LLDB issue: https://discourse.llvm.org/t/debugging-swift-is-just-awful/67866

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.