views:

609

answers:

2

I am working on an app with an NSTextView. When I paste random bytes into it (say, from a compiled C program) it displays gibberish, as it should. However, when I -setShowsControlCharacters:YES, the same causes a crash and gives the following error multiple times:

2008-11-22 00:27:22.671 MyAppName[6119:10b] *** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range or index out of bounds

I created a new project with just an NSTextView with the same property and it does not have this problem.

My question is, how can I debug my app to find the cause of the error? I have no idea where the bug originates. I am not familiar with the debugger built in to Xcode. If anyone could point me in the right direction in terms of how to track down such a bug I would be very grateful. Thanks.

+3  A: 

It's not really clear how much you know so I'm going to try a very basic approach, please don't be offended.

I'm assuming the NSTextView is in a external nib/xib and is not created programatically.
If so open up your nib/xib in Interface builder and click on the offending NSTextView element.

Then hit Cmd-5 or Tools->Connections Inspector

From here you can see which Outlets/Actions to look for in the actual source code.

Most likely you're problem is manipulation of a "Reference Outlet" object, or in a "Sent Action" function.

Once you have an idea of which method(s)/variable(s) you need to watch you can open up the source code for those parts in Xcode. You can then set a break point at any line by click on that line number on the left hand side of the editor window.

Click on Run->Debugger. A new window should popup, click "Run and Go" in the toolbar of that window. Your app should run as normal until it hit's the break point you set in your source code. when the break point is hit you can mouse over variables in your code in the bottom half of the debugger window. The current values of the variables should popup allowing for easy inspection.

Usually you can gain the same information by using the Top Right panel of the Debugger to inspect the variable table.

When your done with that break point click "Continue" at the top of the debugger window and your program will execute till completion or the next break point.

This should get you started with debugging under Xcode. Their are different kinds/uses of break points and all kinds of additional fancy stuff you can do in Xcode. For more information check out this awesome CocoaHeads video.

Good Luck,

Brian G.

Brian Gianforcaro
+9  A: 

Another set of useful breakpoints to set when your debugging Cocoa apps are objc_exception_throw and -[NSException raise]. In 10.5, all exceptions travel through objc_exception_throw but if you're targetting Tiger you should set a breakpoint on both.

There are more debugging techniques at http://www.cocoadev.com/index.pl?DebuggingTechniques.

Ashley Clark
Some more useful breakpoints: CGPostError, malloc_printf, _objc_error.
Ahruman