Application crashes when showing another window, but only when not debugging - swift

I'm an iOS-developer trying to make a small macOS-application.
I have the storyboard, and have two NSWindowController's with their viewController.
When clicking a button in the first (initial) controller, I want the other window (with its viewController).
I have tried two things:
Create a segue between VC1 and Window2, naming it and invoking it.
Instantiate with self.storyboard?.instantiate(..).
Both of these work - when I run this through Xcode. But if I close Xcode and just run the resulting product, it crashes as soon as I try to open the next viewController, saying:
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Termination Signal: Illegal instruction: 4
Termination Reason: Namespace SIGNAL, Code 0x4
Terminating Process: exc handler [32282]
Application Specific Information:
Performing #selector(ok:) from sender _NSPaddedButton 0x600003d0ac40
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 com.my.app 0x000000010d3e998e FirstViewController.openFile(at:) + 734 (SecondViewController.swift:44)
1 com.my.app 0x000000010d3e95e9 closure #1 in FirstViewController.openFile(_:) + 249
I'm now using this to instantiate it:
if let w = storyboard?.instantiateController(withIdentifier: "secondWindowIdentifier") as? NSWindowController{
if let d = w.contentViewController as? SecondViewController{
d.doSomething(someData: myData)
self.presentAsModalWindow(d)
}
}

I figured it out. It wasn't clear in the provided error, but after testing a few more times, I noticed it was sometimes showing a different error. I found one mentioning a WebView I had placed in the next view.
I had simply written import WebKit in the file I needed it, and hadn't added it to linked frameworks in the project's general tab. Not sure why this 100% didn't crash in the debugger, and 100% crashed on product, but oh well.

Related

how do I get assertion messages into crash logs in Swift

so - I use guards and assertions all over the place - I just added a check for something like this:
if start == end
{
fatalError( "can't initialise a date range of zero size but start and end are both \(start)")
}
and it dies correctly - but in my crash report I just see:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libswiftCore.dylib 0x10a61483b _assertionFailure(_:_:file:line:flags:) + 427
1 Weight Waist and Fat 0x102669179 Date_range.init(start:end:) + 1321 (Date_range.swift:42)
...
the word "initialise" doesn't appear anywhere in the entire crash report. How do I put in assertions where I actually get the information I need - do I have to set something? Is fatalError the wrong thing to be using?
The information you want is being pruned for privacy reasons.
https://forums.swift.org/t/intercept-fatalerror-or-other-termination-messages-from-swift/6190
You might be better off throwing an NSException via Objective-C if you insist on having this in the crash log. Personally I just write into the log file with OSLog and pick up the info with the sudo log tool later.

Why do i get exc bad access in cases when object is not nil?

I have an app that receives remote notifications. My view controller that is shown after push has a tableview. App crashes very randomly (1 in 20 tries) at line setting frame:
if (!myTableView) {
NSLog(#"self.myTableView is nil");
}
myTableView.frame=CGRectMake(0, 70, 320, 376);
This only happens when i open the app, then open some other apps and then receive the push notification. I guess it has something to do with memory. I use ARC (ios 5). The strange thing is that nslog is not displayed, so tableview is not nil.
Crash log:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x522d580c
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x352b1f7e objc_msgSend + 22
1 Foundation 0x37dc174c NSKVOPendingNotificationCreate + 216
2 Foundation 0x37dc1652 NSKeyValuePushPendingNotificationPerThread + 62
3 Foundation 0x37db3744 NSKeyValueWillChange + 408
4 Foundation 0x37d8a848 -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:] + 176
5 Foundation 0x37e0ca14 _NSSetPointValueAndNotify + 76
6 UIKit 0x312af25a -[UIScrollView(Static) _adjustContentOffsetIfNecessary] + 1890
7 UIKit 0x312cca54 -[UIScrollView setFrame:] + 548
8 UIKit 0x312cc802 -[UITableView setFrame:] + 182
9 POViO 0x000913cc -[FeedVC viewWillAppear:] (FeedVC.m:303)
Dealloc is not called because it is not logged:
- (void)dealloc {
NSLog(#"dealloc");
}
You are having memory issues. Your tableView is reaching a retain count of zero; so although a pointer to the tableView still exists, the system has trashed the object at that actual address, hence the EXC_BAD_ACCESS.
It's possible that your UI that showed the tableView is hidden and therefore unloaded, but you've left some logic that assumes the table view still exists when it doesn't.
It's hard to debug what's going on without seeing more of the project. The best thing for you to do is have a careful look at the design of your application and UI flow. What would be causing the UI to be be released? How are you entering the code that assumes that part of the UI is still there?
N.B. Sending messages to a nil reference would not generate any errors; this is by language design.
I found the solution here:
Using ARC and UITableViewController is throwing Observation info was leaked, and may even become mistakenly attached to some other object
Seems pull to refresh (subview to tableview) was causing problems.
Do not change frame directly,do something like this.
CGRect frame = self. myTableView.frame;
frame.x =something;
frame.y=something;
myTableView.frame=frame;
and let me know.

Finding a bug using Xcode - attempt to insert nil value

I am getting a random bug in my app, which is causing it to crash. The problem I am facing is XCode doesn't tell me where the crash is happening only the below information. Can someone tell me how I can find out where I might be able to find the problem within the code? It must be crashing at the same point as when the app does crash it always shows the below.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary
setObject:forKey:]: attempt to insert nil value (key: 0)'
* First throw call stack: (0x381e48bf 0x37d301e5 0x381e47b9 0x381e47db 0x381516bb 0x9237b 0x91121 0x8f0c5 0x8abc1 0x37b9d 0x8b68f
0x3550850f 0x381b0577 0x3813c0cf 0x3547c3fb 0x3547dc2b 0x8d005
0x3814322b 0x34495 0x32e31 0x3372d 0x30a59 0x3813e435 0x7b1df 0x7b88d
0x79e25 0x31ca650f 0x31ca5f01 0x31c8c4ed 0x31c8bd2d 0x37f12df3
0x381b8553 0x381b84f5 0x381b7343 0x3813a4dd 0x3813a3a5 0x37f11fcd
0x31cba743 0x89e8b 0x24a4) terminate called throwing an
exception(lldb)
Have you turned on exception breakpoints? Click the + at the bottom left of the Breakpoint Navigator, then select Add Exception Breakpont.
You can right click the breakpoint to edit it…
This will cause the code to stop at the point where the error occurs.
There is a solution that I usually use on my projects, setting up some special breakpoint.
To do that:
Open XCode
Goes to "View -> Navigators -> Show Breakpoint Navigators"
Now add some new breakpoints clicking the "+" button:
Add exception breakpoint
Add Symbolic breakpoint with symbol [NSException raise]
Add Symbolic breakpoint with symbol objc_exception_throw
Run again your project, application will stop on the line that is causing your problems.
error is evident enough. in any of your line, where you are setting some object for dictionary, you key is nill, which is not allowed
your problem is that you are using setObject: forKey Method to NSDictionary just make it NSMutableDictionary then try this method you can't assign value for NSDictionary like you are trying it's only for NSMutableDictionary.
i hope you will get my point what i am try to say.
Try this:
if([myDictionary objectForKey:#"string"]!= nil && [myDictionary objectForKey:#"string"]!= Nil) { }

Finding out source of nsrangeexception

I have an NSRangeException error that does not occur all the time(especially when I am debugging). It comes up randomly, and I am unable to figure out where it is coming from. I have lots of array manipulation, so its difficult to eliminate it that way.
My question is whether I can get a clue , for example class and line number from the debugger output, for example what are the numbers 12109 and 707?:
2012-03-15 09:25:15.303 appname[12109:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
Any pointers appreciated.
Edit:
Remaining part of the log:
*** First throw call stack:
(0x33ff38bf 0x342431e5 0x33f4b275 0x1702d 0x37662331 0x3761b2b3 0x33f4d435 0x373b79eb 0x373b79a7 0x373b7985 0x373b76f5 0x3744de55 0x374e3e45 0x373aa99b 0x33fc7b4b 0x33fc5d87 0x33fc60e1 0x33f494dd 0x33f493a5 0x307cdfcd 0x373ca743 0x2325 0x22e4)
terminate called throwing an exception
Add an Exception breakpoint. This will stop on the line that raises the exception.
In the breakpoints navigator, click the plus in the bottom left, choose exception breakpoint, and all exceptions. You should have this on all projects really, it is invaluable. We'd have far fewer questions here, though!
Look further down in your crash log, and chances are that at some point it will point to an actual method and line.
For e.g. in the below block from a device crash log, the last line clearly points to the request:didLoad: method in the FacebookController.m class at line 264.
0x33defacb -[NSObject doesNotRecognizeSelector:] + 175
0x33dee945 ___forwarding___ + 301
0x33d49680 _CF_forwarding_prep_0 + 48
0x00037e47 -[FacebookController request:didLoad:] (FacebookController.m:264)

My phone app is suddenly crashing because of the main.m file

Suddenly when I try to run my app it immediately goes to the main.m file and shows me this SIGABRT error which I'm going to assume is Signal abort. I just can't figure out why that is happening. And I'm not sure what I am looking at in my debugging area. Please help!
#import <UIKit/UIKit.h>
#import "Mct2AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([Mct2AppDelegate
class]));
}
}
Debugger Output:
Attaching to process 8004.
Pending breakpoint 1 - ""main.m":17" resolved
Couldn't register com.ebonybutler.Mct2 with the bootstrap server. Error: unknown error code.
This generally means that another instance of this process was already running or is hung in the debugger.Current language: auto; currently objective-c
Second Debugger Output since restart:
2012-02-29 16:46:45.561 Mct2[319:f803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "Menu" nib but the view outlet was not set.'
* First throw call stack:
(0x13bb052 0x154cd0a 0x1363a78 0x13639e9 0xd7f0d 0xd83a9 0x2415e2 0xd85cb 0x25e4 0x13bcec9 0x155c2 0x1555a 0xbab76 0xbb03f 0xba2fe 0x3aa30 0x3ac56 0x21384 0x14aa9 0x12a5fa9 0x138f1c5 0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x12a4879 0x12a493e 0x12a9b 0x2032 0x1fa5)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
Simple fix, if you're using iOS simulator, just close it down and rerun the project
This is Happens because your application getting terminate.
Try the following things in the below order:
Make sure all only one program is being debugged in XCode, close other running instances.
shut down your system and restart it before doing third steps.
Restart XCode, most of time this is take care of the issue.
If you are running this on the device and is still not working restart the device and try again.
It Really Work.