First responder crashes in drawRect after processing keyDown
First responder crashes in drawRect after processing keyDown
- Subject: First responder crashes in drawRect after processing keyDown
- From: Tron Thomas <email@hidden>
- Date: Tue, 05 Jun 2007 19:02:06 -0700
I have written a Cocoa application which crashes under certain
conditions, and I don't understand why.
I created an Objective-C++ class name MyView which subclasses
NSView. I created an instance of the view in the main window for the
NIB resource file, and made that view the delegate for the window.
Here in essence, is the source code for the class:
#import <Cocoa/Cocoa.h>
@interface MyView : NSView
{
@private
NSDictionary* _attributes;
}
@end
#import "MyView.h"
@implementation MyView
- (void)awakeFromNib
{
NSFont* font = [NSFont userFontOfSize:64.0f];
_attributes =
[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)becomeFirstResponder
{
return YES;
}
- (void)windowDidBecomeKey:
(NSNotification*)notification
{
NSWindow* window = (NSWindow*)[notification object];
[window makeFirstResponder:self];
}
- (void)keyDown:
(NSEvent*)event
{
::NSLog(@"A key was pressed.");
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)rect
{
NSSize size = [self bounds].size;
NSPoint point = { size.width * 0.5f, size.height * 0.5f };
NSString* text = @"A";
[text drawAtPoint:point withAttributes:_attributes];
}
@end
The program runs fine initially. If I set breakpoint on the first
line of the drawRect method and the type a key after the application
starts up. The debugger will hit the breakpoint.
If I type:
(gdb) print-object _attributes
The debugger response with:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x2125c69c
0xfffeff30 in objc_msgSend_rtp ()
The program being debugged was signaled while in a function called
from GDB.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off"
Evaluation of the expression containing the function
(_NSPrintForDebugger) will be abandoned.
The drawRect method works fine when the application first starts up,
and it is only after typing a key that the _attributes instance
becomes invalid. I don't understand why this happens.
What is needed so that the character can force a redraw after a key
press and still have a valid _attributes instance?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden