Xcode debugger sometimes doesn't display variable values? - xcode

This happens to me pretty often. For example, right now I have the debugger stopped at a breakpoint in a method . . . and it isn't displaying any variable values at all. Other times, it displays some, but not others.
Can anyone explain?

The most common reason for this is that you're trying to debug code compiled with optimisation enabled and/or no debug symbols. Typically this will be because you're trying to debug a Release build rather than a Debug build but it can also happen with Debug builds if you've made inappropriate changes to the Debug build settings.
Another less common possibility is that you've hosed the stack.

I had this issue (using Swift), I spent ages crawling through my git commits to find where to problem started.
For me, I was using Facebook Tweaks library, but I was (unnecessarily) importing it from my project-bridging-header.h file.
Once I got rid of it, I got my debugging back.
for example, in my bridging header I had:
#ifndef PROJECT_Bridging_Header_h
#define PROJECT_Bridging_Header_h
// Facebook Tweaks
#import "FBTweak.h"
#import "FBTweakStore.h"
#import "FBTweakCategory.h"
#import "FBTweakCollection.h"
#import "FBTweakViewController.h"
#import "FBTweakShakeWindow.h"
#endif
I removed all the imports and just imported it as usual in my AppDelegate import Tweaks.
e.g:
#ifndef PROJECT_Bridging_Header_h
#define PROJECT_Bridging_Header_h
// Removed Facebook Tweaks
#endif
and in my AppDelegate.swift
import Tweaks
This fixed all my debugging issues, everything works as expected and I can also using Facebook Tweaks.
Note: I don't think this is an issue with Facebook Tweaks itself, you may have some other library causing the same issue. The idea is to remove things from your bridging-header one by one and see if you can narrow down the issue.
I think I read somewhere that if a library is causing many issues behind the scenes, this can stop your debugger working.
If this doesn't help, try crawling through your git commits and see at what stage the debugging stopped.
other similar issues on SO:
Xcode Debugging not showing values
Xcode debugger doesn't display variable information after installing CocoaPods Podfile
If you're having similar issues hope this helps! 👍

A possible solution is to set the Optimization Level for your current target Debug scheme to none.
Project -> Target -> Build settings -> Optimization level -> Debug (or whatever fits your project) -> None
Source:
https://stackoverflow.com/a/14948486/3590753

I've had similar issues using LLDB. Switching it back to GDB seems to address it. Obviously this isn't solving the problem, but its a workaround anyway

My issue was that I had address sanitizer enabled. Disabling sanitizer resolved my issue in XCode 8.2.1

You can get the value of any variable in the console by writing:
po name_of_an_objectCVar
or
print name_of_a_cVar

If your breakpoint has "automatically continue after evaluating options" set, then it won't write to the variable view - FYI

I know this is old, but i ran into same problem too. I could not see any summaries of any objects, just types and some address code. After 4 hours of struggling with compilers, debuggers and other solutions i was about to give up when by accident i found this option in debugger. "Show Summaries". Just by clicking it everything got fixed and now i see all variable summaries!

Had the same issue using Xcode 6.4 running the app on device. Running on simulator will show all variables on debugging variables panel.

There is a situation I have seen where Xcode can't cope with return value optimisation (RVO) -- if the compiler decides to apply RVO to a variable then it may not appear in the variables list. You can disable this in g++ and clang with the compiler flag -fno-elide-constructors
See also Understanding eliding rules with regard to c++11

For Swift mix OC Project which use pod
Fixing it by removing useless header(that import with framework by pod) xx-Bridging-Header.h
eg.
In the past I import header with #import "GCDAsyncSocket.h" which I was added in podfile
platform:ios, '8.0'
use_frameworks!
target "roocontrollerphone" do
pod 'CocoaAsyncSocket'
end
just remove it in that xx-Bridging-Header.h file

If you are using the #property feature of Objective-C 2.0 the debugger does not display those variables unless they are backed by explicit ivars in your Class interface. This is slated to be fixed in Xcode 4 as I understand it.

temporary solution when it happpen to me :
right click on the property
jump to definition (u can do it manually and scroll to the #synthesize in the top of the file)
now, if the line is like this :
#synthesize myObject = _myObject ;
set the mouse cursor on the "_myObjects". that what worked for me..when i have problems.

I figured out why it is not working in XCode 4.6 - all of the variables in my object, self, were declared in the .m file instead of the .h. When I moved one of them back to the .h file, it showed up in the debugger. Sounds like a bug with XCode in that it cannot "see" variables declared in the implementation file.

For me it works changing the content of display variables panel to Local Variables and then back to Auto.
This solution worked on XCode 6.3.2, Swift type project.

You need to disable two types of build optimizations in the build settings. By default, the "swift compiler - code generation" optimization level for debug build is set to fast. You need to set this to none. Also check that the "apple llvm 7.1 - code generation" optimization is set to none for debug build.
Finally, check that you are building the debug build in the "architectures" section of your build settings.
Hope this helps.

I have been stuck a while with this problem and finally find out a solution.
I think that many reason can causes this bug but in my case here is the solution.
While you are in the breakpoint position check the included classes.
I was including using double quote a file which was located using include path.
#include "MyClass.h"
instead of
#include <MyPorject/MyClasses/MyClass.h>
So if you have this problem try to double check your inclusion and import.
I know it seems weird but worked for me and I have been able to reproduce it by putting back the Double-Quote include.

One possible reason for the debugger displaying seemingly wrong values is that the variable type is of Any?.
E.g.
var a: Any? = 12
var b: Int? = a as? Int // b=13483920750
var c: Int = a as? Int ?? 0 // c=1
In the example above, b holds the correct value of 1 even though it is not displayed as such.

I've had the same issue and I fixed it by reinstalling all Pods. Just delete them and install again.

Related

The breakpoint will not currently be hit. Unable to set requested breakpoint on target

Im working on Arduino Uno board recently im stuck with my code, i couldnt debug using print() in ArduinoIde.So i downloaded AtmelStudio 6.2 for debug purpose.
when i set the breakpoint and try to build .Im getting the warning
The breakpoint will not currently be hit. Unable to set requested breakpoint on target.The current selected deviceis unable to set breakpoints during runtime
please help me sort this issue
Following workarounds worked with the same problem using ATMega 168P on Atmel Studio 7 with Atmel-ICE.
1. Assembler
Insert the following assembler code where you want your breakpoint:
asm("break");
Please note, this is a really ugly solution and not suitable for all situations. It only works with DEBUGwire and makes your program stop in any case, even if no programmer is attached.
2. Create new project
Creating a new project at a different location helped as well. I copied all the required files to the new folder. The new location has a short path (C:\atmel\project...) and contains no spaces, no umlauts etc.
I had a similar problem, the difference was that I could only hit breakpoints in the original modules of my project (i.e. those already existent when I created the .cproj), any modules I added later wouldn't have the program stopped in breakpoints placed on them.
The solution (2) mentioned by #pafodie worked to solve this, but in the process I found a simpler way: just delete the .atsuo file. It will later be automatically recreated, and the problem disappears (at least until you add more modules). It seems AS6 caches something there that isn't updated when new files are added, or does it incorrectly.
I might've found a solution that works, for me at least! You need to disable compiler optimization. In Atmel Studio,
Hit Alt+F7 > ToolChain > Optimization {there are 2 Optimization
windows but only one fits the shoes} > Optimization level > None
I found it here, explained better than I did: https://www.microchip.com/webdoc/GUID-ECD8A826-B1DA-44FC-BE0B-5A53418A47BD/index.html?GUID-8FF26BD2-DBFF-48DD-91FB-8585D91A938D figure 5
If using external Makefile, make sure the -g (debug) flag is set in CFLAGS.
Otherwise, Atmel Studio would have no idea how the source files correspond to the compiled binary.

`po` gives `error: <EXPR>:1:1: error: use of unresolved identifier`

I have this problem with po in the console where trying to output a function works in itself, but outputting a variable or constant doesn't.
As you can see here, although you'd think the var/let holds the content of bar.boy(), Swift somehow can't find it...
So it turns out there was probably a bug in the past that when you were adding a Swift file it would add/ask a Bridging header, but it wouldn't add that line to your project
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
which means you'd stay in this state
resulting in error: <EXPR>:1:1: error: use of unresolved identifier!
I could only find that out because I moved around files in my project and when I added ObjC files to the project, it asked me about a Bridging header (although I had one already!) and luckily added that SWIFT_OPTIMIZATION_LEVEL. One could consider this is a bug to consider the default value is fastest, but then again I guess this was only a bug in the past and got fixed now.
Still, I fixed now it might be a bug the other way around, if it add none in the release build. I can't test this right now because for testing this I only had a Debug build. I'll leave that as an exercise :) for Apple's Engineers.
I set Swift Complier to "-O"
SWIFT_OPTIMIZATION_LEVEL = "-O";
and set it back to "-Onone".
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Then it works.
Fixed after setting:
Optimization Level = None[-Onone] under Swift Compiler - Code Generation
Note that setting Optimization Level under Apple LLVM 8.0 - Code Generation has no effect
I bet that bug is due to the fact that foo has been optimized out during compilation and it's symbol does not exist anymore in the compiled code (even if it shouldn't have in debug and it's still an LLDB bug)
Probably if you add some usage of foo in the next lines (even a println) its symbol will hopefully be kept in the IR and you'll be able to po foo
(I agree that that's still a bug but at least if it works you'll have a workaround and some sense of explanation)
I encountered a similar issue but I already had the correct configuration, all optimisations set to None. However I still didn't get any values.
After some further digging I found out that the issue originated from Xcode being unable to the resolve the types:
(lldb) frame variable self
<could not resolve type>
That led me to this question: xcode 8 Debugger 'Could not resolve type' where the issue is described and the bridging header seems to cause issues.
With my project not having any bridging headers I studied the build settings once more and found this setting
This was initally set to YES. After changing the it to NO symbols are working again.
SWIFT_INSTALL_OBJC_HEADER = NO
If your project is using Swift, there are two separate "Optimization Level" settings in the project/target configuration. Its not only "SWIFT_OPTIMIZATION_LEVEL". Check this link: https://stackoverflow.com/a/32984193/2060180
I'll leave it here in case it's useful to someone.
For those who are using Swift framework in an Objective-c project, and wanna debug the Swift source files in that project: (In my case, I have a mixed-language module which managed by Cocoapods, I need to debug the swift module in my Objective-c Example project)
It works for me after I added an Empty Swift file in my Objective-c project.
Otherwise, those swift compiler options won't show up in the Build Settings.

Xcode shows many errors but program compiles and runs fine (in both simulator and device)

After installing the CocoaLumberjack' log compressor class I've been getting this annoying behavior: Xcode complains that there are many undeclared identifiers and gives me many errors (not warnings but errors with the red icon).
The thing is that I can compile and run my iPad app just fine but Xcode won't do any autocompletion. I tried cleaning the build folder (Product > option + Clean), and also deleting derived data. I've also rebooted to no avail.
As you can imagine this is a pain to work with. I did have this behavior happen before on a previous version of Xcode; it had something to do with stuff in my precompiled headers file but using the solution above would always fix it. I'm currently using Xcode 4.4 (4F250).
Sample error I'm getting:
Semantic Error: use of undeclared identifier 'DDTTYLogger'
The above happens even with classes that I wrote myself and that have not changed since installing the CocoaLumberjack compressor class.
I finally solved this after MANY attempts using the following:
Remove the last #import from my Prefix.pch and build again. Errors would happen (obviously). Put the line back and build again. No errors would show and after 10 seconds or so, errors would come back again.
Repeat the above except instead of the last #import, remove the last TWO imports, then three, four, etc. I did this until I removed five imports and when I put them back and waited, Xcode stopped complaining.
Note that this didn't occur to me at all. I read this solution on a blog somewhere.
Weird bug...
Open build settings and set "Precompile Prefix Header" to "No", that solved my problem.
Kudos for: https://stackoverflow.com/a/7035492/936957
I've been running into these issues constantly on all the latest versions of Xcode, in both Objective-C and Swift.
I noticed today that I was getting the errors in one particular class file. I removed it from some extra targets it was in and the errors finally went away!
I think Xcode has some fundamental bugs with it's handling of multiple targets right now. My theory is that if the other target is not built, you will essentially see errors from that target. Anyway hope this helps someone.
Not bad,
If you follow these Steps-
1-Clean Xcode(Cmd+Shif+K).
2- Clear Derived Data(Cmd+Shift+G).
Enter this path( ~/Library/Developer/Xcode/DerivedData/).
3- Quit and open again Xcode.
This problem can cause by setting "Target Membership" for some files are not the same.
Example:
A class XYZ put in file "a.swift" and it's used in file "b.swift". But "Target Membership" setting of "a.swift" is not the same "Target Membership" setting of b.swift.
Check "Target Membership" setting as below:
I was having issues with a library installed via cocoapods. Going to Build Settings and searching for 'Allow Non-modular Includes In Framework Modules' then setting it to Yes did the trick.
I had it on Xcode 10.1 when I accidentally pressed:
Cmd+Shift+U - ( build for Testing )
try Clean (Cmd+Shift+K) and then:
Cmd+Shift+R ( build for Running )
After update to Xcode 11, I met the same problem. I tried all the mentioned advices (cleaning folders, turning on/off different settings, restarting xCode), but nothing helped. Also, I have a big project in C, so, I'd like to keep using precompiled headers.
Finally I found that simple restart of Mac OS solves the problem! It's really weird behaviour, but I'm happy anyway that I found a solution – it's hard to code when lots of colourful error messages float around.
For me it helped cleanning the project. XCode->Product->Clean
I got the similar type of issue.
Alternate option to fix this is
Open organizer and delete the derived data of your project or delete all the projects in organizer projects tab. It works fine..
I just had the same thing in Xcode 5.1.
I fixed it by making sure there were no blank lines between #import's
I have removed some extra spaces and extra lines from .pch file and it xcode stopped complaining
This happened to me as well, but cleaning didn't fix it. What did was quitting and reopening XCode. Afterwards, all the phantom errors were gone. For those wondering, the tabs you have open when you close will still be open when you reopen.
I had this issue recently. It can be remedied in some cases by deleting the ModuleCache folder inside DerivedData, along with the project folder in DerivedData. Note that Xcode must be quit before doing this.
Running on Xcode Version 10.1 (10B61), I set the build setting "Increase Sharing of Precompiled Headers" to NO. I was working in an .xcworkspace with many projects sharing the same frameworks, and no Objective-C bridging header (meaning I've added no obj-c code myself). I'm not sure when Xcode did away with .pch files by default, but I didn't have any of those in my project.
Open up a terminal and create a nice little function accessible via the command line...
nano ~/.bashrc
add (making the necessary substitutions between the pointy braces)
cycle() {
git stash save "BACKUP"
git checkout <<SOME OTHER BRANCH>>
git branch -D $1
xcodebuild -allowProvisioningUpdates -workspace <<YOUR WORKSPACE>>.xcworkspace -scheme <<YOUR SCHEME>> -configuration Release clean
git checkout $1
}
^X and save it by following the prompts, then enter source ~/.bashrc to make it visible to the current terminal session.
Make sure your branch is pushed to origin, cause we're going to delete it :)
Call the function using cycle <<MY BRANCH>> (once it's run you might want to call git stash pop to restore any working copy changes)
Hope it works for you! Xcode, get on your game!
In my case (mixed objc/swift project) at least part of errors were caused by absence of imports for some used frameworks, e.g. "import UIKit". Project was compiled successfully because frameworks were anyway included in headers in Prefix.pch file. But errors were shown, for example about not finding method defined in UILabel extension, and yes, this extension was without "import UIKit". So I think these errors in most cases depends on Prefix.pch precompilation/updating.
Touching Prefix.pch, cleaning, removing derived data, closing/opening XCode sometimes helps, but not always.
Using SPM, it can be that the Module you import, which you use at the location of the errors, has missing dependencies itself.
In my case I had an error about a missing initializer, XCode trying to use a different one than the one already defined in the other Module. But the project compiled fine! Nothing worked at first. Then I tried first cleaning, and then Product -> Perform Action -> Compile "myfile.swift" and it showed what the missing dependency in the dependency was! Note: This goes recursive. In multiple places a depenendency may have not been declared in the package.swift -> compile the same file repeatedly until it compiles successfully.
So.. it wasn't that the module with the error had a missing dependency, but rather that the direct dependency did miss a Module-dependency declaration in its package.swift declaration.
My explanation is that to generate the Error-output of XCode, Modules get compiled alone on their own. Import errors then show up. But when Modules are built as part of a larger Module, then that large module may import the missing dependency of the broken sub-Module already, making the build pass.

Mac - Adding CoreGraphics.framework, for CG use in a C header

Xcode 3.25, Mac OS X 10.6, 10.5 compatibility required.
I have a Mac Xcode project, which mixes Cocoa and C/C++. Some legacy modules require a C-only header.
I created a C header file: myCTypes.h
I wish to use a CGPoint in that header.
Compiling generates an error: CGPoint is not defined. OK, no problem, so I'll just:
#include: "<CoreGraphics/CoreGraphics.h>"
Unfortunately, I get this:
error: CoreGraphics/CoreGraphics.h: No such file or directory
Hmm. OK, so I'd best add the framework. But if I right-click on the frameworks group within Xcode, and try to "Add an existing framework", CoreGraphics does not show up in the list. Grr.
So I try to add it manually, navigating my way to System/Library/Frameworks. Nup, not in there either.
So I look in the 10.5 SDK paths, and once again, there's a lot of CoreXXX frameworks in there, but no CoreGraphics.framework.
Can someone please tell me what I'm doing wrong here? What do I need to use CGGraphics in that header? Cheers.
EDIT
This was solved by:
#include <Carbon/Carbon.h>
(But if anyone wants to tell me if this is good practice or not, please feel free. Cheers.)
Core Graphics is part of Application Services, so that's the header you should include:
#include <ApplicationServices/ApplicationServices.h>
You can include Carbon.h if you want, but its total length after preprocessing is very long, so you should include it only when you absolutely have to (e.g., for Carbon Events hotkey stuff) in order to keep your build times down.
Well i know this topic is super old but i wanted to answer pqnet`s question in the comments (can't comment on it because I have less than 50 rep points :( )
now to the point: Yes, today anyway, you can add only core graphics framework if you like.
Click on your project in the project navigation pane; build phases; link binary with libraries. Then, chose coreGraphics; then click add.
Thats all, enjoy.
I managed to include some of the functions required by my project with this:
#include <CoreGraphics/CGWindow.h>
But I guess other functions are in other headers under CoreGraphics
Additional info:
Catalina
Xcode 11
PS: if some of Apple guys read this, please, add headers in your documentation!! And examples!!

Xcode 3.2: Build & Analyze never finds any issues

I've used the Clang Static Analyzer from the command line before. I wanted to try Xcode's built-in version via Build & Analyze. I never get any negative results even though i specially prepared my code with very obvious issues Clang was always able to point out:
// over-releasing an object:
[label release];
[label release];
// uninitialized vars, allocating but not freeing an object
NSString* str;
int number;
CCLabel* newLabel = [[CCLabel alloc] initWithString:str fontName:str fontSize:number];
[newLabel setPosition:CGPointZero];
The result is always the same: a green checkbox, no issues.
I read that C++ code can cause issues. I'm running this with cocos2d that includes box2d. Could this be a cause? Did anyone get results from Build & Analyze with the cocos2d engine? What else could it be?
I also tried enabling the Static Analyzer Build Settings and then Build but the result was the same. I have restarted Xcode, cleaned all targets and emptied Xcode caches to no avail.
UPDATE: my issue could be caused by having added cocos2d as a cross-project reference to my project. Analyzing the cocos2d project itself seperately reveals some analyzer results.
In addition i found out that i get Analyzer results from my RELEASE build configurations but not from DEBUG builds.
I'm using Cocos2d and Box2d, and I get plenty of warnings from Build and Analyze. Check to make sure your project's compiler is set to GCC 4.2 under "Compiler Version" in the Build Settings.
For anyone that might come across this thread (and for the OP in case the problem persists):
I had this problem when building for the Simulator. However, switching to Device build got the Analyzer running.
This site might shed some more light on the issue: http://useyourloaf.com/blog/2010/10/5/xcode-build-and-analyze-broken-for-simulator.html
I also get plenty of Analyze warnings with my Cocos2d game. I cleaned most of them up, but cocos2d 0.99.1 has 3 built-in! (Which should be easily fixed.)
I have noticed that sometimes the analyzer doesn't find things unless I have that particular file open when I run it... go figure.

Resources