"Thread 1: stopped at breakpoint" error when initializing an NSURL object - objective-c

I am following the Stanford University iOS development course on iTunes U.
In one of the demos (that I have been trying to follow), there is this code that loads the property list from an NSURL and returns it as NSMutableDictionary.
-(NSMutableDictionary *) words
{
NSURL *wordsURL=[NSURL URLWithString:#"http://cs193p.stanford.edu/vocabwords.txt"];
words=[[NSMutableDictionary dictionaryWithContentsOfURL:wordsURL] retain];
return words;
}
The application is successfully built, but at runtime it gives the following error and gets stuck:
I can't figure out what the problem is. Can you please help?

You're stopped at a breakpoint. That's a debugging tool, not an error. See the blue arrow/tab in the left margin, where the line numbers are? Drag that away and drop it anywhere (you'll see a "poof") to remove it, then run your project again.
You can also deactivate all breakpoints by typing ⌘-Y, the key equivalent for the menu item Debug>Deactivate Breakpoints, or you can view all your breakpoints in the Breakpoint Navigator (hit ⌘-6).
When execution stops like this, you can continue from the breakpoint, either by typing continue at the debugger prompt in the Console:
(lldb) continue
Or hitting the "Play" button in the debugger controls. You can also type Control-⌘-Y, which is the equivalent for the menu item Debug>Continue.

This isn't an error. You just set a breakpoint (probably without knowing it).
Drag the little blue Chevron in the column at the left out of the way. You will see it disappear and go poof, and then you can rebuild your app and you should see it run properly.
Now, that said, I think there are some memory management mistakes in your code, but we can return to those later. ;-)

The program is stopping because you have a breakpoint.. That's the blue arrow on the left of the code. Right-click it and delete.

Related

Objective-C: Trouble returning an NSString in my method [duplicate]

I am following the Stanford University iOS development course on iTunes U.
In one of the demos (that I have been trying to follow), there is this code that loads the property list from an NSURL and returns it as NSMutableDictionary.
-(NSMutableDictionary *) words
{
NSURL *wordsURL=[NSURL URLWithString:#"http://cs193p.stanford.edu/vocabwords.txt"];
words=[[NSMutableDictionary dictionaryWithContentsOfURL:wordsURL] retain];
return words;
}
The application is successfully built, but at runtime it gives the following error and gets stuck:
I can't figure out what the problem is. Can you please help?
You're stopped at a breakpoint. That's a debugging tool, not an error. See the blue arrow/tab in the left margin, where the line numbers are? Drag that away and drop it anywhere (you'll see a "poof") to remove it, then run your project again.
You can also deactivate all breakpoints by typing ⌘-Y, the key equivalent for the menu item Debug>Deactivate Breakpoints, or you can view all your breakpoints in the Breakpoint Navigator (hit ⌘-6).
When execution stops like this, you can continue from the breakpoint, either by typing continue at the debugger prompt in the Console:
(lldb) continue
Or hitting the "Play" button in the debugger controls. You can also type Control-⌘-Y, which is the equivalent for the menu item Debug>Continue.
This isn't an error. You just set a breakpoint (probably without knowing it).
Drag the little blue Chevron in the column at the left out of the way. You will see it disappear and go poof, and then you can rebuild your app and you should see it run properly.
Now, that said, I think there are some memory management mistakes in your code, but we can return to those later. ;-)
The program is stopping because you have a breakpoint.. That's the blue arrow on the left of the code. Right-click it and delete.

Why would my application fail to "unhide" (show)

I'm investigating an issue on Mac OS X 10.8, and I am at the end of my wits. I'm not sure what to do next.
The application is 32 bit and has some Carbon calls in it.
Here is the problem: when I right-click the application icon in the dock, select the menu item "Hide", then, after the application has hidden, I select the "Show" menu item from the dock, and the problem occurs: the main document window does not appear (the palettes and menu do appear).
At this point, the "Show" menu item does not change to "Hide" even though the palettes have become visible.
I expect that the main document window becomes visible when I select "Show" from the application dock menu. Just like other Mac applications.
When it fails, I can make the main document window visible again if I use the App Exposé gesture on the Trackpad to show the document windows and select the main document window.
It works fine if I launch the application from the Terminal or from Xcode. The document window shows and the dock menu item for my application changes to "Hide" as expected. I launch the app from the terminal by navigating to the parent directory of the *.app, and typing ./MyApp.app/Contents/MacOS/MyApp.
It fails when I have launched by double-clicking the application icon in the Finder.
My log messages from the application delegate's unhide functions appear when the application is launched from the Terminal and Xcode, but not when launched from the Finder.
– applicationWillUnhide:
– applicationDidUnhide:
I have looked in the Console.app for any exceptions thrown (or any other messages). There are none.
Update:
To try and debug this, I launched from the Finder, and used Xcode to attach to the process.
I had suspected that an exception was being thrown and when I tested it using Xcode's "Exception Breakpoint" as well as putting a breakpoint on objc_exception_throw (just in case), it dd not break when I hide or "show" the application.
I then thought that I needed to prove that the NSApplicationWillUnhideNotification and NSApplicationDidUnhideNotification were being sent out. They are when I launch from Xcode or from the Terminal, but if I launch from the Finder, they are not.
I verified this by, after attaching Xcode to the application, putting a breakpoint via "Add Symbolic Breakpoint" for:
-[NSNotificationCenter postNotificationName:object:userInfo]
And then, I added a Debugger Command: "po * (id*) ($esp+12)" to print out the first parameter to that selector (the notification name).
Which I found in an answer posted here, in StackOverflow.
Using that, I can see the notifications that are posted after I choose the "Show" menu item. When I launch from Xcode/Terminal, I see the following notifications posted:
NSApplicationWillUpdateNotification, NSWindowDidUpdateNotification, NSApplicationDidUpdateNotification, ** NSApplicationWillUnhideNotification **, ..., ** NSApplicationDidUnhideNotification **, ..., NSApplicationWillBecomeActiveNotification, ...
NSApplicationWillUnhideNotification is posted in this situation.
When I launch from the Finder, I see the following notifications are posted:
NSApplicationWillUpdateNotification, NSWindowDidUpdateNotification, NSApplicationDidUpdateNotification, NSApplicationWillBecomeActiveNotification, ...
It does not send the NSApplicationWillUnhideNotification. Also, when I select "Show" from the Xcode-launched version, I see -[NSApplication _doUnhideWithoutActivation] in the backtrace. Putting a breakpoint for that function when I attach to the Finder-launched version does not result in a break when I select "Show".
Then, I thought to myself, perhaps the application thinks that it is not hidden.
I have a idle event handler, so from there I printed out the value of [[NSApplication sharedApplicaton] isHidden] while I Hide and "Show" the application.
For the problem situation, when the application is not hidden, it prints out NO for isHidden. When the application becomes hidden, it prints out YES for isHidden. When I select "Show" from the dock menu, it continues to print out NO for isHidden. It knows that it is hidden, but part of the application has been activated: the NSPanels and the NSMenuBar appear.
I can see the document window by entering into the application Exposé mode, and clicking the document window will make the window appear, but the dock menu item is still "Show" and isHidden is still YES.
The unhide mechanism works fine for a sample application, so I'm pretty sure that our code is doing something to shut this off.
I wonder what would be different between an application launched from the Terminal compared to an application launched from the Finder?
I had the application log the environment variables using [[NSProcessInfo processInfo] environment] and the only real difference I could see is that PWD exists in the variables for the Terminal application: I cannot see anything in our code that makes use of that.
I had the application log the command-line arguments via [[NSProcessInfo processInfo] arguments], and I do see something different in the Finder-launched version. Both the Terminal and Finder launched versions list the path of the binary as the first argument; the Finder also lists a second paramter, "-psn_0_89445704". I have read online that it is something that Mac OS X adds to command-line arguments for GUI applications and I see it added to the command-line arguments for other applications that Hide and Show properly from the Dock menu.
Do you have any other thoughts that may lead me further towards solving this mystery? Thanks for any help or suggestions!
After working with Apple Engineers in AppKit, a solution has been found.
In our application, we "flush" the event queue for various reasons via this method:
NSEvent* lastEvent = [NSEvent otherEventWithType:NSPeriodic
location:NSMakePoint(0.0, 0.0)
modifierFlags:0
timestamp:[NSDate timeIntervalSinceReferenceDate]
windowNumber:1
context:NULL
subtype:0
data1:0
data2:0];
[[NSApplication sharedApplication] discardEventsMatchingMask:NSAnyEventMask beforeEvent:lastEvent];
The Mac OS X systems sends a "Show" event to the application on launch. Our flush function, which is called upon launch, effectively removes that event from the queue, but the core process part of Mac OS X has its own internal queue that keeps track of show and hide and other types of event types so that it doesn't send repeated messages. (I'll be investigating if this flush is really necessary)
The problem is that when discardEventsMatchingMask:NSAnyEventMask is called on every event, it clears out the events for the application, but doesn't respond to the core process's show event and so core process thinks that it doesn't need to send the show event again.
The solution to this particular problem is to be more selective in which events are cleared. With my new implementation, I do not clear events that will be sent by core process.
/* a bug in Apple's Core Process group forces me to isolate which events should be cleared as
show|hide|activate|deactivate messages get sent by Core Process, but are not _marked_ as
handled and so Core Process thinks that the "Show" event is still pending and will not send
another */
NSEvent* lastEvent = [NSEvent otherEventWithType:NSPeriodic
location:NSMakePoint(0.0, 0.0)
modifierFlags:0
timestamp:[NSDate timeIntervalSinceReferenceDate]
windowNumber:1
context:NULL
subtype:0
data1:0
data2:0];
NSEventMask maskForEventsToDiscard = (NSPeriodic |
NSLeftMouseDown |
NSLeftMouseUp |
NSMouseMoved |
NSLeftMouseDragged |
NSRightMouseDragged |
NSMouseEntered |
NSMouseExited |
NSKeyDown |
NSOtherMouseDown |
NSOtherMouseUp |
NSOtherMouseDragged);
[[NSApplication sharedApplication] discardEventsMatchingMask:maskForEventsToDiscard
beforeEvent:lastEvent];
As the "show" event is not cleared on launch, show and hide work now!
A special thanks to KF of Apple!
This technique won't fit in a comment, but I might suggest getting up-close-and-personal with DTrace. I suggested in the comments above to subclass NSWindow and put NSLog statements in the -orderOut:, etc. methods. However, using DTrace for this would likely be far more effective - although, as you will see, it will still be useful to know the address of the objects you will be observing - the upside is that you won't litter your code with a bunch of NSLog statements.
The simplest script might be:
#pragma D option quiet
objc$target:NSWindow:-orderOut?:entry
{
printf( "%30s %10s %x %x\n", probemod, probefunc, arg0, arg1 );
}
and would be called with the process id of the application by doing something like:
sudo dtrace -s dtrace_window.d -p9434
In this particular case, arg0 will contain the address of the window. Unfortunately, it is apparently non-trivial to obtain the title of the window or even the contents of a NSString from within DTrace, but may be worth the effort. I do have a question here and here to see if anyone knows to do either one of these things. (If you can get the title of window, you can set up a map from the address of the window to a string.)
It would be easy to attach probes to any and all methods, functions, etc. you think might be involved so you can try to "follow the event" to resolve this problem.
So, ultimately, I would suggest to just keep adding DTrace probes until something provides the required hint resolve this problem.

Xcode 4.1 NSZombie stops EXC_BAD_ACCESS without any information

I'm new to NSZombie but I have a problem with my app crashing with EXC_BAD_ACCESS. I'm having real trouble finding the object that is causing the problem. Looking online I followed instructions to enable zombie objects.
The problem I'm having has been reported by another person on that page. Enabling zombie objects stops the bad access error but gives me no information. Nothing is printed by NSLog. Is there somewhere else I should look, or am I doing something wrong? I don't really know my way round XCode or a Mac very well as I'm mainly a C# programmer.
Any help would be most welcome.
1) Run your app on Intruments.
2) In instruments, select Object allocations tool(automatically selected if you select leaks tool).
3)Click on the little "i" on top left, within the Allocations tool.
4) Select "Enable NSZombie detection".
5) Press the record button and let your app run.
6) Go through the execution of the app untill it crashes. As soon as there is a crash, you'd see a pop up saying that there was a EXC_BAD_ACCESS. Click on the little -> on the pop up to see the object that has turned into a zombie and the line of code responsible.
Sorry I am unable to upload a screenshot, as am at work.
Expand your project Executable and right click on it. and click on GetInfo->Argument tag aat end of the window you see plus and minus sign button click on + sighn button and write
Name Value
NSZombieEnabled YES
then after execute your project and whenever crash your application click on run munu-> console you see there why your application crash. please try this may be this will help you.

XCode 4 iOS - Debugger shows bytecode instead of telling me what line my app crashed

When my app crashes, instead of seeing what line caused, I see a window that prints all unreadable byte code. I used to be able to see what line it crashed on, but I must have changed something. Here's a screen shot:
What setting can I change to have XCode show me where my app crashed?
UNcheck the "Show Disassembly When Debugging" menu option:
The inverse of the above; if you want to show disassembly for the current debug location, you can use this drop-down menu:
The actual crash may not necessarily be in your code. The debugger is going to point you to the machine instruction that caused the crash. It may be in a cocoa-touch method or OS call which crashed because of a bad parameter you passed in (an invalid pointer is a common culprit).
Because the debugger does not have access to the source for the code that actually crashed, it will show you the disassembled machine code. What you need to do is follow the call stack backwards until you reach your code. That should point you to the line of code in your app which (indirectly) caused the crash.

Xcode debugger vs NSLog - dictionary keys and values

I'm trying to get better at using the debugger rather than just always printing my debugging message out via NSLog. This morning I thought I had a great use for it - to find how the changed values of a KVO dictionary. But when I set a breakpoint just after the appropriate code and ran with debugging on, all I saw in the debugger was this in the summary:
{(int)[$VAR count]} key/value pairs
and expanding the item never gets to any key or value.
But when I print out the "change" dictionary via NSLog I get the key and the value:
[timestamp] appName[3643:707] observeValueForKeyPath new filePath change: {
kind = 1;
new = foo;
I guess I have two questions:
Is there a way to get dictionary keys and values with the Xcode debugger?
Is there a set of rules of thumb for using the debugger vs printing log messages?
I'm (still) using Xcode 3.2.6.
Thanks!
EDIT: I realize that "foo" is not a valid file path. At this point I'm still testing sending a value around via KVO.
Set a breakpoint on the spot you want to check the value of the variable and run your application. When the breakpoint gets tripped put your mouse over the variable in the debugger. A little popup should come up, move your mouse over the arrows on the left side and another menu should popup. Click on "Print Description" and it should display something similar to a log in the console.
It is just a case by case method but a lot of it is just personal preference. You will just have to find what works for you.
If I am going to check a variable a lot at a one specific point, I will go with a log. It is usually the quickest way to see if something is working right
If I am checking how a part of the code affects a variable I will use the debugger and step through the code. It is also useful for checking the values of lots of variables in a section of code.
You can using a future set, which defenetely working in Xcode4
When u setup breakpoint, where is a option to don't stop execution of program and write to log anything what u want.
I'm not sure if it working well in Xcode 3 bcs I familiar with 4 but here is work.