Crash in drawRect - what causes it? - objective-c

Some of my users are getting this crash.
As far as I can tell, it's somehow connected to the -drawRect: method of my subclassed NSTextView, but I can't see what might cause it, and stress testing has failed to shake out the bug.
Code for drawRect
- (NSRange)visibleRangeOfTextView:(NSRect) rect {
NSLayoutManager *layoutManager = [self
layoutManager];
NSTextContainer *textContainer = [self
textContainer];
NSRange glyphRange, characterRange;
// first transform to text container coordinates
NSPoint containerOrigin = [self textContainerOrigin];
rect.origin.x -= containerOrigin.x;
rect.origin.y -= containerOrigin.y;
// next, compute glyph range
glyphRange = [layoutManager glyphRangeForBoundingRect:rect inTextContainer:textContainer];
// finally, compute character range
characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
return characterRange;
}
- (NSRect)rectForCharacterRange:(NSRange)charRange
{
NSRect rect = [self
firstRectForCharacterRange:charRange];
rect.origin = [[self window]
convertScreenToBase:rect.origin];
rect = [self convertRect:rect fromView:nil];
if (!rect.size.width) rect.size.width = 6.0;
return rect;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSLog(#"Marking it");
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSRange visible = [self visibleRangeOfTextView:dirtyRect];
NSRange last = NSMakeRange(visible.location, 0); while (true) {
NSRange error = [appController rangeOfMisspelledWordInString:self.string onlyInRange:visible startingAt:last.location + last.length];
last = error;
if (error.location == NSNotFound) {
break;
}
[arr addObject:[NSValue valueWithRange:error]];
}
NSLog(#"Spellchecked");
[[NSColor redColor] setStroke];
CGFloat dash[] = {2.0f, 2.0f} ;
// Make the text ranges and mark them
for (NSValue *val in arr) {
NSRange range = [val rangeValue];
NSRect rectInTextView = [self rectForCharacterRange:range];
NSRect toDraw = rectInTextView;
NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath setLineDash:dash count:2 phase:0];
NSPoint lineStart = toDraw.origin;
lineStart.y += toDraw.size.height;
NSPoint lineEnd = lineStart;
lineEnd.x += toDraw.size.width;
[aPath moveToPoint:lineStart];
[aPath lineToPoint:lineEnd];
[aPath stroke];
};
NSLog(#"Done");
}
Stack trace:
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Application Specific Information:
objc[5751]: garbage collection is ON
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:]: Range or index out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff91387fc6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8d4d7d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff91387dfa +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff91387d84 +[NSException raise:format:] + 116
4 AppKit 0x00000001005d842c -[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:] + 131
5 AppKit 0x00000001006288ec -[NSLayoutManager(NSPrivate) _drawBackgroundForGlyphRange:atPoint:parameters:] + 910
6 AppKit 0x00000001006277a2 -[NSTextView drawRect:] + 1913
7 Skrivest√∏tte 0x000000010000b56c Skrivest√∏tte + 46444
8 AppKit 0x0000000100626e66 -[NSTextView _drawRect:clip:] + 2545
9 AppKit 0x00000001004a985d -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 3020
10 AppKit 0x00000001004aa34e -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 5821
11 AppKit 0x00000001004aa34e -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 5821
12 AppKit 0x00000001004a39af -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 4755
13 AppKit 0x000000010049c395 -[NSView displayIfNeeded] + 1528
14 AppKit 0x00000001004a1592 -[NSClipView _immediateScrollToPoint:] + 6533
15 AppKit 0x000000010049fb75 -[NSClipView scrollToPoint:] + 239
16 AppKit 0x000000010058f637 -[NSScrollView scrollClipView:toPoint:] + 266
17 AppKit 0x000000010058f3da -[NSClipView _scrollTo:animateScroll:flashScrollerKnobs:] + 1497
18 AppKit 0x00000001005923b7 -[NSClipView _scrollTo:animate:] + 27
19 AppKit 0x0000000100bcd5a2 __-[NSScrollView _snapRubberBand]_block_invoke_2 + 1536
20 AppKit 0x0000000100b3fc4e ____NSPeriodicInvokerScheduled_block_invoke_2 + 53
21 libdispatch.dylib 0x00007fff907b98ba _dispatch_call_block_and_release + 18
22 libdispatch.dylib 0x00007fff907bbc07 _dispatch_after_timer_callback + 16
23 libdispatch.dylib 0x00007fff907be2b6 _dispatch_source_invoke + 635
24 libdispatch.dylib 0x00007fff907baf77 _dispatch_queue_invoke + 71
25 libdispatch.dylib 0x00007fff907bb6f7 _dispatch_main_queue_callback_4CF + 257
26 CoreFoundation 0x00007fff9131d06c __CFRunLoopRun + 1724
27 CoreFoundation 0x00007fff9131c676 CFRunLoopRunSpecific + 230
28 HIToolbox 0x00007fff93ab831f RunCurrentEventLoopInMode + 277
29 HIToolbox 0x00007fff93abf5c9 ReceiveNextEventCommon + 355
30 HIToolbox 0x00007fff93abf456 BlockUntilNextEventMatchingListInMode + 62
31 AppKit 0x000000010045ff5d _DPSNextEvent + 659
32 AppKit 0x000000010045f861 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
33 AppKit 0x000000010045c19d -[NSApplication run] + 470
34 AppKit 0x00000001006dab88 NSApplicationMain + 867
35 Skrivest√∏tte 0x0000000100001020 Skrivest√∏tte + 4128
36 ??? 0x0000000000000002 0x0 + 2
)

* Terminating app due to uncaught exception 'NSRangeException', reason: '*
[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:]: Range or index out of bounds'
It is clearly a NSRange exception, as O' y said

I think you need add assert function to check the "range"
NSRange range = [val rangeValue];
make sure range will not be NSNotFound.

Related

SpriteKit: NSInvalidArgumentException - Attempted to add a SKNode which already has parent

I'm creating a game in SpriteKit using Objective-C. I am trying to generate blocks for the screen to be used in the game.
I'm getting an application crash on start up:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'Block-0.000000-440.000000' texture:['nil'] position:{0, 440} size:{64, 64} rotation:0.00'
*** First throw call stack:
(
0 CoreFoundation 0x00891df6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x0051ba97 objc_exception_throw + 44
2 CoreFoundation 0x00891d1d +[NSException raise:format:] + 141
3 SpriteKit 0x00e8c94c -[SKNode addChild:] + 119
4 Hidden Route 0x0003b3c1 -[GameScene didMoveToView:] + 2417
5 SpriteKit 0x00e6031b -[SKScene _didMoveToView:] + 97
6 SpriteKit 0x00e7b157 -[SKView presentScene:] + 283
7 Hidden Route 0x00039e9e -[GameViewController viewDidLoad] + 526
8 UIKit 0x0115ad54 -[UIViewController loadViewIfRequired] + 771
9 UIKit 0x0115b045 -[UIViewController view] + 35
10 UIKit 0x0105f477 -[UIWindow handleStatusBarChangeFromHeight:toHeight:] + 879
11 UIKit 0x0106252d +[UIWindow _noteStatusBarHeightChanged:oldHeight:forAutolayoutRootViewsOnly:] + 273
12 UIKit 0x01006ad1 __79-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:]_block_invoke + 163
13 UIKit 0x010862c6 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 494
14 UIKit 0x01086701 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 115
15 UIKit 0x0100699e -[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:] + 506
16 UIKit 0x01006128 -[UIApplication _updateCurrentStatusBarViewControllerAppearance] + 286
17 UIKit 0x0105223a -[UIWindow _updateContextOrderingAndSetLayerHidden:] + 548
18 UIKit 0x0105319e -[UIWindow _setHidden:forced:] + 257
19 UIKit 0x01053473 -[UIWindow _orderFrontWithoutMakingKey] + 49
20 UIKit 0x01061615 -[UIWindow makeKeyAndVisible] + 80
21 UIKit 0x00ffecd6 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3108
22 UIKit 0x0100216d -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
23 UIKit 0x0101ad30 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
24 UIKit 0x01000d7f -[UIApplication workspaceDidEndTransaction:] + 155
25 FrontBoardServices 0x06f909de __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
26 FrontBoardServices 0x06f9046f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
27 FrontBoardServices 0x06fa2425 __31-[FBSSerialQueue performAsync:]_block_invoke + 26
28 CoreFoundation 0x007b57a0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
29 CoreFoundation 0x007ab0b3 __CFRunLoopDoBlocks + 195
30 CoreFoundation 0x007aaf0b __CFRunLoopRun + 2715
31 CoreFoundation 0x007aa1ab CFRunLoopRunSpecific + 443
32 CoreFoundation 0x007a9fdb CFRunLoopRunInMode + 123
33 UIKit 0x01000744 -[UIApplication _run] + 571
34 UIKit 0x01003e16 UIApplicationMain + 1526
35 Hidden Route 0x0003c41d main + 141
36 libdyld.dylib 0x02fa0ac9 start + 1
37 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I've looked at the stack trace and found that the method causing the issue is my didMoveToView: method, which is this:
-(void)didMoveToView:(SKView *)view
{
// ViewDidLoad
[self initialize];
/* Setup your scene here
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:#"Arial"];
myLabel.text = #"Hello, World!";
myLabel.fontSize = 65;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
*/
player = [SKSpriteNode spriteNodeWithColor:[AppDelegate getRandomColor] size:CGSizeMake(40, 40)];
player.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self generateBoard];
[self generateNextAtIndex:kNeed - 3];
int width = (self.frame.size.width) / [AppDelegate numberOfColumns];
int height = (self.frame.size.height) / kNeed;
for (int i = 0; i < boardRows.count; i++)
{
Row *current = [boardRows objectAtIndex:i];
for (int j = 0; j < current.blocks.count; j++)
{
Block *block = [current.blocks objectAtIndex:j];
block.anchorPoint = CGPointMake(0, 1);
block.position = CGPointMake((j * width), self.frame.size.height - (i * width));
[block setName:[NSString stringWithFormat:#"Block-%f-%f", block.position.x, block.position.y]];
NSLog(#"Position for item %i", i);
block.size = CGSizeMake(width, width);
[self addChild:block];
}
}
[self addChild:player];
}
I have a feeling that it's something to do with the self addChild:block line however I can't figure out a solution. If you need any more information I'll post it under here (just ask for it)
Thanks for your help
EDIT 1:
generateBoard method:
- (void)generateBoard
{
int rnd = 2;
int from = rnd;
int to = 0;
Row *row = [defaultRows objectAtIndex:rnd];
[boardRows replaceObjectAtIndex:0 withObject:row];
for (int i = 2; i < boardRows.count; i++)
{
rnd = arc4random() % [AppDelegate numberOfColumns];
to = rnd;
Row *rowThis = [defaultRows objectAtIndex:rnd];
Row *rowBelow = [[Row alloc] initWithSize:CGSizeMake(self.frame.size.width, self.frame.size.height) need:kNeed];
[rowBelow generateWithPoint:from toPoint:to];
[boardRows replaceObjectAtIndex:i withObject:rowThis];
[boardRows replaceObjectAtIndex:i-1 withObject:rowBelow];
to = from;
from = rnd;
// to make it add 2
i++;
}
}
EDIT 2:
After adding in the Exception Breakpoint, it revealed that the line causing the issue is the self addChild:block line
The answer to this was really simple, basically I had some pre configured blocks to make the generation easier, however in doing this, calling self addChild:block was actually trying to add the same block (with the same memory address) into 2 places at once, and therefore was crashing the app.
I just added a duplicate function to my Block class and started to implement that rather than calling the block directly.

How to add an SKNode to an SKScene

When I enter this line in initWithSize:Size
[self addChild:world];
I retrive an error Thread 1:signal SIGABRT
world is an SKNode
The children that should be in world are also SKNode's
What am I doing wrong here as when I add the SKNode to the scene I retrieve an error
initWithSize:Size Method:
-(id)initWithSize:(CGSize)size{
if (self = [super initWithSize:size]){
//self.size = CGSizeMake(2048, 1048);
self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
_character = [self createCharacter];
[self addChild:_character];
[self addChild:world];
[self createChunks];
[world addChild:_character];
NSLog(#"Children in world: %#",[world children]);
}
return self;
}
createChunks: Method:
-(void)createChunks{
ProceduralMapGeneration *procedure = [[ProceduralMapGeneration alloc]init];
[procedure allocArrays];
_chunkOne = [procedure createChunk];
_chunkOne.position = CGPointMake(self.scene.size.width/2, self.scene.size.height/2);
[world addChild:_chunkOne];
_chunkTwo = [procedure createChunk];
_chunkTwo.position = CGPointMake(400,200);
[world addChild:_chunkTwo];
}
create chunks adds the chunks to world
world is created here
#implementation MainGame{
SKNode *world;
SKSpriteNode *_character;
SKNode *_chunkOne;
SKNode *_chunkTwo;
ProceduralMapGeneration *categoryBitManager;
}
Stack Trace
*** First throw call stack:
(
0 CoreFoundation 0x019161e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x016958e5 objc_exception_throw + 44
2 CoreFoundation 0x01915fbb +[NSException raise:format:] + 139
3 SpriteKit 0x0117cfca -[SKNode addChild:] + 175
4 Giraffe Rampage 0x0000289d -[MainGame initWithSize:] + 413
5 SpriteKit 0x0115ca09 +[SKScene sceneWithSize:] + 104
6 Giraffe Rampage 0x0000555d -[MainMenu touchesBegan:withEvent:] + 429
7 SpriteKit 0x0116e2bf -[SKView touchesBegan:withEvent:] + 824
8 UIKit 0x00274c0b -[UIWindow _sendTouchesForEvent:] + 386
9 UIKit 0x002759d1 -[UIWindow sendEvent:] + 1117
10 UIKit 0x002475f2 -[UIApplication sendEvent:] + 242
11 UIKit 0x00231353 _UIApplicationHandleEventQueue + 11455
12 CoreFoundation 0x0189f77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13 CoreFoundation 0x0189f10b __CFRunLoopDoSources0 + 235
14 CoreFoundation 0x018bc1ae __CFRunLoopRun + 910
15 CoreFoundation 0x018bb9d3 CFRunLoopRunSpecific + 467
16 CoreFoundation 0x018bb7eb CFRunLoopRunInMode + 123
17 GraphicsServices 0x0390a5ee GSEventRunModal + 192
18 GraphicsServices 0x0390a42b GSEventRun + 104
19 UIKit 0x00233f9b UIApplicationMain + 1225
20 Giraffe Rampage 0x00005e6d main + 141
21 libdyld.dylib 0x01f5d701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
[self addChild:_character];
...
[world addChild:_character];
You added _character twice.

Getting error after execute NSMutableArray

Here is the code. I tried to create a object which includes the name, size and image(get from the resource) and then I add that object into NSMutableArray in order to show those object in NSTableView. However, after I run the code, I did get the correct counts of object in NSMutableArray and correct information in each object(name,size,image), but it still gives me error. Could you guys help me. I am a new Mac developer. Thanks !!
Here is the code:
#implementation AppDelegate{
NSMutableArray* _tableContents;
}
-(void)awakeFromNib{
NSString* imagePath = #"USB.png";
NSString* mountPath = #"/Volumes";
_tableContents = [[NSMutableArray alloc]init];
for(mountPath in [[NSWorkspace sharedWorkspace]mountedLocalVolumePaths])
{
if(0==[mountPath rangeOfString:#"/Volumes/"].location)
{
NSString* USBpath = [[[NSWorkspace sharedWorkspace]mountedLocalVolumePaths]objectAtIndex:count];
//get the name of USB drive
NSString* nameOfUSB = [USBpath substringFromIndex:9];
//get the size of USB drive
NSNumber *volumeSize;
NSURL *mountPathPicked = [NSURL fileURLWithPath:mountPath];
if([mountPathPicked getResourceValue:&volumeSize forKey:NSURLVolumeTotalCapacityKey error:nil])
{
NSDictionary *obj = #{#"name":nameOfUSB,
#"image":[NSImage imageNamed:imagePath],
#"size":volumeSize
};
//after I call the following line, it will give me error !!
[_tableContents addObject:obj];
}
}
count ++;
}
}
//I also use the following two methods in order to show the name, and image in NSTableView
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [_tableCountents count];
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSDictionary *flag = _tableContents[row];
NSString *identifier = [tableColumn identifier];//call different col we need identifier
if([identifier isEqualToString:#"MainCell"])
{
NSTableCellView *cellView = [tableView makeViewWithIdentifier:#"MainCell" owner:self];//give back specific cell named mainCell
[cellView.textField setStringValue:flag[#"name"]];
[cellView.imageView setImage:flag[#"image"]];
return cellView;
}
return nil;
}
There is no connection problems in GUI. And here is the error message:
2013-04-25 01:16:41.321 NAMEOFPROJECT[8243:303] An uncaught exception was raised
2013-04-25 01:16:41.322 NAMEOFPROJECT[8243:303] *** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 3]
2013-04-25 01:16:41.329 NAMEOFPROJECT[8243:303] (
0 CoreFoundation 0x00007fff9147cb06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff900c03f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff9142fb53 -[__NSArrayI objectAtIndex:] + 163
3 CDTOANYDRIVE 0x00000001000011d6 -[AppDelegate DetectingUSBDrives] + 710
4 CDTOANYDRIVE 0x0000000100000efb -[AppDelegate awakeFromNib] + 43
5 AppKit 0x00007fff899631a8 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1234
6 AppKit 0x00007fff89b79976 -[NSNib _instantiateNibWithExternalNameTable:] + 610
7 AppKit 0x00007fff89b7a41a -[NSNib instantiateNibWithExternalNameTable:] + 17
8 AppKit 0x00007fff89b262b4 -[NSTableRowData _unarchiveViewWithIdentifier:owner:] + 520
9 CDTOANYDRIVE 0x000000010000174b -[AppDelegate tableView:viewForTableColumn:row:] + 219
10 AppKit 0x00007fff89b279a2 -[NSTableRowData _addViewToRowView:atColumn:row:] + 324
11 AppKit 0x00007fff89b2766f -[NSTableRowData _addViewsToRowView:atRow:] + 151
12 AppKit 0x00007fff89b25c05 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
13 AppKit 0x00007fff89b2598a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
14 AppKit 0x00007fff89b24c59 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
15 AppKit 0x00007fff89b247f1 -[NSTableRowData updateVisibleRowViews] + 119
16 AppKit 0x00007fff89afc5d7 -[NSTableView layout] + 165
17 AppKit 0x00007fff89aafe95 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
18 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
19 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
20 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
21 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
22 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
23 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
24 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
25 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
26 AppKit 0x00007fff89aafd2e -[NSView layoutSubtreeIfNeeded] + 615
27 AppKit 0x00007fff89aab4dc -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
28 AppKit 0x00007fff899d96a8 -[NSView _layoutAtWindowLevelIfNeeded] + 99
29 AppKit 0x00007fff899d8fe6 -[NSView _sendViewWillDrawInRect:clipRootView:] + 87
30 AppKit 0x00007fff899a59b1 -[NSView displayIfNeeded] + 1044
31 AppKit 0x00007fff89a62a48 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1377
32 AppKit 0x00007fff89a62068 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 940
33 AppKit 0x00007fff89a61c4f -[NSWindow orderWindow:relativeTo:] + 159
34 AppKit 0x00007fff89963266 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1424
35 AppKit 0x00007fff8994214d loadNib + 317
36 AppKit 0x00007fff89941679 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 219
37 AppKit 0x00007fff899414ae -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 200
38 AppKit 0x00007fff8994128e +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 360
39 AppKit 0x00007fff8993da2f NSApplicationMain + 398
40 CDTOANYDRIVE 0x0000000100000e82 main + 34
41 libdyld.dylib 0x00007fff9385a7e1 start + 0
42 ??? 0x0000000000000003 0x0 + 3
)
And the above error shows twice !!
I have tried your code in a mini-app and It works fine. There are few things that I might highlight:
Your exception occurs in the method DetectingUSBDrives, for which we don't have any code. I'm assuming that you have pasted the code in the awakeFromNib method.
You are using count as an index. Is this varibale initialized at zero?
The fact that the error appears twice suggests that your cell views hold a reference to your app delegate, which means that awakeFromNib is called once for each view creation. This can create many problem since you are initializing _tableOfContents there. You might want to move all that code to the init method instead.

NSString drawInRect:withAttributes: causes 'unrecognized selector sent to instance ...'

I got something really strange: a method causes unrecognized selector sent to instance, but doesn't NSLog() anything at all.
I made a custom NSControl subclass, where I try to draw my custom cells like this:
- (void)drawRect:(NSRect)dirtyRect {
NSLog(#"DrawRect entered!");
[[NSColor grayColor] set];
[NSBezierPath fillRect:[self bounds]];
unsigned int i, count = [cells count];
NSRect cellRect = NSMakeRect(0, 0, ([self bounds]).size.width, cellHeight);
for (i = 0; i < count; i++) {
NSLog(#"Drawing cell %d at: %#", i, NSStringFromRect(cellRect));
MKMenuCell *cell = [cells objectAtIndex:i];
[cell drawWithFrame:cellRect inView:self];
cellRect.origin.y += cellHeight;
}
}
And -[MKMenuCell drawWithFrame:inView:]:
- (void)drawWithFrame:(NSRect)bounds inView:(NSView *)controlView {
NSLog(#"-drawWithFrame:inView:");
NSMutableDictionary *strAttribs = [[NSMutableDictionary alloc] init];
[strAttribs setObject:[NSColor blackColor] forKey:NSFontAttributeName];
[name drawInRect:bounds withAttributes:strAttribs]; // "unrecognized selector sent ..." is caused by this method call.
}
The output:
2013-03-11 18:46:54.823 MacOverflow[738:a0f] DrawRect entered!
2013-03-11 18:46:54.826 MacOverflow[738:a0f] Drawing cell 0 at: {{0, 0}, {176, 30}}
2013-03-11 18:46:54.826 MacOverflow[738:a0f] -drawWithFrame:inView:
2013-03-11 18:46:54.827 MacOverflow[738:a0f] Drawing cell 1 at: {{0, 30}, {176, 30}}
2013-03-11 18:46:54.828 MacOverflow[738:a0f] -drawWithFrame:inView:
2013-03-11 18:46:54.828 MacOverflow[738:a0f] -[NSCachedWhiteColor screenFontWithRenderingMode:]: unrecognized selector sent to instance 0x100512920
2013-03-11 18:46:54.829 MacOverflow[738:a0f] An uncaught exception was raised
2013-03-11 18:46:54.831 MacOverflow[738:a0f] -[NSCachedWhiteColor screenFontWithRenderingMode:]: unrecognized selector sent to instance 0x100512920
2013-03-11 18:46:54.834 MacOverflow[738:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCachedWhiteColor screenFontWithRenderingMode:]: unrecognized selector sent to instance 0x100512920'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff88c26784 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff858ddf03 objc_exception_throw + 45
2 CoreFoundation 0x00007fff88c80110 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0
3 CoreFoundation 0x00007fff88bf88ef ___forwarding___ + 751
4 CoreFoundation 0x00007fff88bf4a38 _CF_forwarding_prep_0 + 232
5 AppKit 0x00007fff89afcfae +[NSStringDrawingTextStorage _fastDrawString:attributes:length:inRect:graphicsContext:baselineRendering:usesFontLeading:usesScreenFont:typesetterBehavior:paragraphStyle:lineBreakMode:boundingRect:padding:scrollable:] + 402
6 AppKit 0x00007fff896db539 _NSStringDrawingCore + 1588
7 MacOverflow 0x00000001000159b7 -[MKMenuCell drawWithFrame:inView:] + 229
8 MacOverflow 0x0000000100015f1e -[MKMenuControl drawRect:] + 595
9 AppKit 0x00007fff896d6cc5 -[NSView _drawRect:clip:] + 3390
10 AppKit 0x00007fff896d5938 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1325
11 AppKit 0x00007fff896d5ca2 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2199
12 AppKit 0x00007fff896d5ca2 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2199
13 AppKit 0x00007fff896d5ca2 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2199
14 AppKit 0x00007fff896d400a -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 767
15 AppKit 0x00007fff896d3b2c -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 254
16 AppKit 0x00007fff896d03de -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 2683
17 AppKit 0x00007fff89649c0e -[NSView displayIfNeeded] + 969
18 AppKit 0x00007fff89611c3b -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1050
19 AppKit 0x00007fff896117d2 -[NSWindow orderWindow:relativeTo:] + 94
20 AppKit 0x00007fff895dd974 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1726
21 AppKiProgram received signal: “SIGABRT”.
t 0x00007fff895dba91 loadNib + 226
22 AppKit 0x00007fff895dafa1 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 248
23 AppKit 0x00007fff895dadd9 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 326
24 AppKit 0x00007fff895d835b NSApplicationMain + 279
25 MacOverflow 0x0000000100000ef9 main + 33
26 MacOverflow 0x0000000100000ed0 start + 52
27 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
As you can see MKMenuCell's drawWithFrame:inView: is called. but the output from its NSLog() statement isn't displayed. What is happening here? How to solve it?
EDIT: I now realize, looking at the GDB output, the first time nothing goes wrong. This makes it even stranger.
The bad selector being sent, screenFontWithRenderingMode, is a method on NSFont. I'm betting that you shouldn't be setting a color for the value of the key NSFontAttributeName. You should probably give it a font.

SIGABRT while adding objects to an NSMutableArray

I have a method which returns an NSArray with points to be plotted. Following is the code for my method.
- (NSArray *)pointsToPlot:(GraphView *)requester
{
NSMutableArray *points = [[NSSet alloc] init];
CGPoint midPoint;
midPoint.x = self.graph.bounds.origin.x + self.graph.bounds.size.width / 2;
midPoint.y = self.graph.bounds.origin.y + self.graph.bounds.size.height / 2;
//Find points to plot
NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x - 10, midPoint.y)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x, midPoint.y - 10)];
NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x + 10, midPoint.y)];
[points addObject:point1];
[points addObject:point2];
[points addObject:point3];
[points autorelease];
return points;
}
I am getting SIGABRT as soon as I add point1 to the points array.
Following is the stack trace obtained:
2011-10-27 10:43:36.939 Graphing Calculator[7056:207] -[__NSSet0 addObject:]: unrecognized selector sent to instance 0x4b57900
2011-10-27 10:43:36.943 Graphing Calculator[7056:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSet0 addObject:]: unrecognized selector sent to instance 0x4b57900'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc95a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1d313 objc_exception_throw + 44
2 CoreFoundation 0x00dcb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d3a966 ___forwarding___ + 966
4 CoreFoundation 0x00d3a522 _CF_forwarding_prep_0 + 50
5 Graphing Calculator 0x000073cd -[GraphViewController pointsToPlot:] + 1037
6 Graphing Calculator 0x00006895 -[GraphView drawRect:] + 965
7 UIKit 0x00053187 -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
8 QuartzCore 0x016b3b5e -[CALayer drawInContext:] + 143
9 QuartzCore 0x016bfe47 _ZL16backing_callbackP9CGContextPv + 85
10 QuartzCore 0x0160d1f7 CABackingStoreUpdate + 2246
11 QuartzCore 0x016bfd24 -[CALayer _display] + 1085
12 QuartzCore 0x016b627d CALayerDisplayIfNeeded + 231
13 QuartzCore 0x0165b0c3 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 325
14 QuartzCore 0x0165c294 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x0165c46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 CoreFoundation 0x00daa89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
17 CoreFoundation 0x00d3f6e7 __CFRunLoopDoObservers + 295
18 CoreFoundation 0x00d081d7 __CFRunLoopRun + 1575
19 CoreFoundation 0x00d07840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00d07761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x010011c4 GSEventRunModal + 217
22 GraphicsServices 0x01001289 GSEventRun + 115
23 UIKit 0x00029c93 UIApplicationMain + 1160
24 Graphing Calculator 0x000021d9 main + 121
25 Graphing Calculator 0x00002155 start + 53
)
terminate called after throwing an instance of 'NSException'
Could someone please explain me what am I doing wrong here?
Change
NSMutableArray *points = [[NSSet alloc] init];
to
NSMutableArray *points = [[NSMutableArray alloc] init];
You are creating and initializing an NSSet and storing it to a reference for NSMutableArray.
Change the following line:
NSMutableArray *points = [[NSSet alloc] init];
To this line:
NSMutableArray *points = [[NSMutableArray alloc] initWithCapacity:3];