How do I get XCode to break on all console / log messages? - objective-c

I have a message from it seems Core Foundation printing into the debug console in XCode (OS X app), "CGContextGetCompositeOperation: invalid context 0x0. This is a serious error....".
I don't know where in my app this line is being generated. (big app)
I tried breaking on NSLog(), CFLog() syslog(), printf(), fprintf, and none of those breakpoints stop on that output. All of those breakpoints work if I test them, etc.
Is there a symbol I can break on that will stop every time a message is printed to the console?
Or is there some function that Apple uses that I can break on?

Related

Xcode 10.2 Lldb RPC server has crashed [duplicate]

I am getting a message in my debugger:
The LLDB RPC server has crashed. The crash log is located in ~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server'. Please file a bug and attach the most recent crash log.
In my case the LLDB RPC server consistently crashed every time I ran my app, even after cleaning the build folder and removing and reinstalling Xcode (Version 8.3.3 (8E3004b)) completely.
It turned out that apparently LLDB took objection to a breakpoint I had set, just moving this breakpoint by a line resolved the issue.
Make sure you are not running the app in release mode, if it is in release mode then change it to debug.
In my case: I update to Xcode Version 9.3 (9E145) recently and Xcode execute to the line with breakpoint then I type "po XXX" commend it will show the same message.
I try to delete following files
~/Library/Preferences/com.apple.dt.Xcode.plist
~/Library/Caches/com.apple.dt.Xcode
and it solved.
not knowing exactly why but worth to try.
remember to backup those files in order to recovered in case any unexpected situation occur.
I had the same problem and fixed it after I deleted some of the breakpoints. Not sure why this happen at all, but at least you can remove breakpoints and use some NSLog() or print() if you are in Swift and debug with the help of those. Good luck!
Clearly a lot of different causes for this, but for me I was using a DispatchGroup to keep track of multiple async tasks.
I had forgotten to call dispatchGroup.enter() before one of the async tasks (but still calling dispatchGroup.leave() when it finished).
Adding this in fixed the crash for me.
If workspace has lots of breakpoint then it will happen, So try to remove all the break point and see the magic.
For me just restarting the simulator worked.
I found the solution to this issue. I don't know is this correct or not, but this solution is work for me. what I did is Actually I connected two devices to my mac mini, in one device I run the app and get the above error in my console. Then I removed one device and tried, this time I didn't get any error in my console its worked fine. I think you guys won't believe this, I tried Almost 3 time with two devices and one device its only work for one device
This error occurs for different reasons and the main one is when you add a watch app later to your project where Xcode adds an extra build target to scheme. click on scheme section in right side of "run/stop buttons" then hit on edit scheme, hit on Build section which is the first one, There you see 2 targets one has 2 sub targets which includes watch app and watch extension in it and the other one has no sub targets and it is a watch app target.
Solution is simple delete the watch app target which has no sub targets and run the app again.
For me, I had an expression in my watch list that it was barfing on. When looking at the crash logs in Console, there was something like this on the reported crashed thread which gave it away:
lldb_private::EvaluateExpressionOptions const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, lldb_private::ValueObject*) + 619
17 com.apple.LLDB.framework 0x0000000102855f18 lldb::SBFrame::**EvaluateExpression**(char const*, lldb::SBExpressionOptions const&) + 696
18 lldb-rpc-server 0x00000001025e72e9 rpc_server::_ZN4lldb7SBFrame18EvaluateExpressionEPKcRKNS_19SBExpressionOptionsE::HandleRPCCall(rpc_common::Connection&, rpc_common::RPCStream&, rpc_common::RPCStream&) + 169
19 lldb-rpc-server 0x00000001025f8ce1 rpc_common::Connection::PrivateHandleRPCPacket(rpc_common::RPCPacket&, rpc_common::RPCPacket&, bool&) + 1553
20 lldb-rpc-server 0x00000001025fc36d Packets::ProcessPackets() + 1005
21 lldb-rpc-server 0x00000001025fbe96 Packets::ReadThread() + 214
22 lldb-rpc-server 0x00000001025fbdb9 Packets::RunReadThread(void*) + 9
23 libsystem_pthread.dylib 0x00007fff6a586109 _pthread_start + 148
24 libsystem_pthread.dylib 0x00007fff6a581b8b thread_start + 15
I ran across this same error with zero idea of what to do next. I tried the accepted answer and my project didn't have any breakpoints at all.
Turns out I had an observer that I didn't remove and every few times I would push off/on the vc that contained it it would eventually crash with the op's error. I had to enable zombies to figure out which vc was causing the error. I had to manually go through the code line by line to realize I didn't remove the observer. Once I removed it everything worked fine.
// not removing this caused the error
playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status),
options: [.old, .new],
context: &playerItemContext)
I have found a solution for this, this may not be the perfect but kind a fix my problem.
Go to target Build Settings -> Other Swift Flags -> check Debug Values added
Remove everything except $(inherited) and -DDEBUG
Remove Derived Data
Clean and Run
I am having this issue in Xcode 12.1.1 (12A7605b) in January 2021 on macOS Catalina with a Swift project.
I tried Clean, delete Derived data, restarting mac, running on different simulators and real devices - no luck.
Others suggest removing the breakpoint, but for me this breakpoint is needed for debugging (I guess I can figure out how to debug in a different way, with a differently placed breakpoint or with print statements, but that's frustrating).
I filed a bug report with Apple as the error message suggest - I urge others to do the same to increase the chance of a fix by Apple.
In the meanwhile I use this workaround - wrap the code where you want the breakpoint in a DispatchQueue.main.async:
DispatchQueue.main.async { [self] in
print("Put the breakpoint on this line")
}
(Note we use [self] here because it's just debug code, but in most cases these async calls need [weak self] to avoid retain cycles and memory leaks)
The problem I encountered was that the main project was objC, the Swift library introduced by Cococapods, and then crashed during breakpoint debugging of the Swift library
The solution
Add a swift class to the main project, name it at will, and do not need to add the bridge header file
Switch to project Settings, not Target Settings, and find build_setting
SWIFT_ACTIVE_COMPILATION_CONDITIONS adds a DEBUG as follows:
SWIFT_ACTIVE_COMPILATION_CONDITIONS
Debug Debug
Release
In my case. I'm also using SQLite.swift to create database. The crashing happened when I tried to change a column data type of an existing table in code(which was not in the right way to do it), then inserted a tuple with new data type, then tried to print all the tuple out.
Solution: Delete the .sqlite3 database file you have or delete the table with conflict data type and recreate them all.

Why is my AUv3 randomly disappearing with "viewServiceDidTerminateWithError"?

I’ve encountered a strange problem during iOS AUv3 Instrument development which I’m having trouble finding information on. I’ve spent a couple weeks now attempting to debug this, and I was wondering if anyone else has encountered it. It feels more like an "OS killed the app" than a crash, so I use the term crash loosely. Here are the symptoms:
It doesn't seem to ever happen on initial instantiation; it's when a project is reloaded. But, you can comment out setFullState and it'll still occur.
The “crash” is inconsistent. Like a lot of slippery bugs, it’ll start happening consistently and then it’ll stop happening completely.
When it does happen, it initially appears to load fine: the view loads and looks correct, and it produces audio. But, after a couple seconds, the plugin view disappears and it stops producing audio. I see this in the console log:
viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}
You don’t have to generate any MIDI events to crash it. It would crash even after I commented out the processing block.
If anyone has any ideas, including how to get some useful debugging info out of this situation, let me know!
I tried to look at crash logs, but there are no crash logs generated
didReceiveMemoryWarning is not tripped. In fact, I did an experiment where I allocated 100 megs and memset’d it on startup, and it didn’t crash it.
I tried attaching a debugger, but it goes from “no debug session” to “waiting to attach” after it crashes-- at no point does it actually attach. It will attach if I then reload the plugin, but then it won’t crash. The Zombies and Leaks tools didn't reveal anything, nor did the memory sanitizers.
Using a Storyboard instead of a XIB didn't change the behavior (not sure why it would, but it was one of the last few differences between this and Apple's example).
I created a very tiny example project which exhibits the problem (unfortunately, I cannot post any code which shows the problem because I cannot find where exactly the problem code is-- maybe it's even a project setting?). It has a minimal GUI, no setFullState, and generates white noise:
https://drive.google.com/open?id=1dw3xTHn3qY411eXLaIb_9_S5PAtrr5Nk
Expected: It doesn't crash or disappear randomly
Actual results: After reloading a project file which uses this AUv3, the plugin will disappear after a few seconds. It initially produces audio normally and the GUI looks OK, but then vanishes.

Why does this Objective C call appear to hang?

A friend of mine discovered some strange behaviour with NSDictionary, and I'm curious as to why it happens. Consider the following code:
NSDictionary *dict = [[NSDictionary alloc] init];
// Oops, we can't mutate an NSDictionary
[dict setObject:[[NSNull alloc] init] forKey:#"test"];
NSLog(#"Set");
The code produces a warning upon compilation that "'NSDictionary' may not respond to 'setObject:forKey:'". That's all well and good, and if you run it anyway, you'll get this output in the console:
-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
Again, exactly what you'd expect to happen. However, at this point the app does not crash or terminate due to an uncaught exception. The setObject:forKey: method simply never returns, and the app appears to hang; the following NSLog is never executed. If you try to step over or into the method using GDB, debugging just seems to end, but without any explicit error message. The app continues to run, but the debugger provides no clue as to where in the code the execution is "stuck."
What's going on here? What is the app actually doing in this case, and why doesn't it crash with an NSInternalInconsistencyException or something of the like?
Edit: For those who have asked, I'm running XCode 4.1 on OS X Lion (10.7.2), building with "Apple LLVM compiler 2.1." I'm using all of the default settings you get with a new Cocoa project in XCode 4. I experience the same non-crashing behaviour regardless of whether I debug the program or just "Run" it. Changing from Debug building to Release building makes no difference. I can even locate the .app file manually in Finder and double click on it to execute it outside of XCode, and it still does not crash.
Exceptions do not crash AppKit programs. NSApplication installs a default exception handler that catches exceptions that your code doesn't. Then you just go back into the runloop as normal.
Lots of apps exhibit this behaviour. It's a common cause of inexplicable blank views/windows. If an exception happens before a view manages to finish drawing, the view will be blank, but the app won't crash. Exceptions only cause a crash if you deliberately change the default exception handler to crash.

How to output warnings to the console during a build in Xcode?

How do I output custom warnings to the console in Xcode? I want to do this to remind myself that I'm in a particular build mode.
I tried #warning "Hello World" but I don't see it in the console.
Any suggestions?
Edit:
I don't want to use NSLog because I am already using it to log a bunch of stuff so when I use NSLog for warnings, it is difficult to see all the warnings.
#warning directive
The #warning directive is to see the message on the compiler. This is very handy to know that something must be changed before deploying or simply to remember to change a piece of code that can be improved (but you don't have the time to do so now). (go to View -> Navigators -> Show Issue Navigator on your project and you will the see the list of warnings). These messages will not appear on the console.
The Apple System Log facility
What you want is to show a warning to the console while the app is running and here is where The Apple System Log facility comes to the rescue.
You have 8 logging levels:
0 Emergency.
1 Alert
2 Critical
3 Error
4 Warning
5 Notice
6 Info
7 Debug
Code sample:
#include <asl.h>
...
asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
Since I was in the same situation than you once, and to make things simple, I use this wrapper https://github.com/MikeWeller/MWLogging in all my projects so my debug errors won't show when I send my App to the App Store but other critical error will.
Update: Now with Swift I use this debug log framework https://github.com/DaveWoodCom/XCGLogger
You can use NSLog
In state of your warning use something like:
NSLog(#"Hello World");
Or you can use the standard C printf function:
printf("Hello World");
If you want to be printed on stderr you can use fprintf redirected to stderr.

Objective-c error "-[CFString retain]: message sent to deallocated instance 0x4593540"

Im using phonegap and currently I have a webapp with php and javascript / html running inside of an iframe. It worked for a while but now it crashes with this error when I load the page
-[CFString retain]: message sent to deallocated instance 0x4593540
any ideas? thanks
You have a retain count problem. From another Stack Overflow answer:
First, go back and reread the memory
management rules just to make sure
you are not missing anything obvious
Next, turn on NSZombieEnabled (in your
executable settings, Arguments panel,
add an environment variable
NSZombieEnabled set to YES).
Double releasing when it shouldn't be happening
Here's a helpful post about NSZombieEnabled
The static analyzer might find the issue (Build menu >> Build and Analyze).
Also, enable "Stop on Objective-C Exceptions" under the Run menu (activate breakpoints first). Then run the app and it will stop when it hits the exception. Then you can use the debugger to find the line where the error occurred.