I picked up programming with cocoa again but I am getting an unexpected crash. Even Xcode crashes on this sometimes.
This code is part of a CATextLayer subclass. I want to observe when changes happen to the layer. The app crashes when observing fontSize and foregroundColor, but not for string and font. The crash occurs in dealloc.
Aren't you supposed to observe self with self?
Why is the layer even being dealloc'ed?
This code will crash it.
This code is part of a CATextLayer subclass. I want to observe when changes happen to the layer. The app crashes when observing fontSize and foregroundColor, but not for string and font. The crash occurs in dealloc.
Aren't you supposed to observe self with self?
Why is the layer even being dealloc'ed?
Code:
-(id) init
{
self = [super init];
if (self)
{
#if 1
//observers
for (NSString *keyPath in [NSArray arrayWithObjects:@"string",@"font",@"fontSize",@"foregroundColor", nil])
{
[self addObserver:self
forKeyPath:keyPath
options:NSKeyValueObservingOptionNew
context:NULL];
}
#endif
}
return self;
}
-(void) dealloc
{
#if 0
#warning xcode crash!!!!!
for (NSString *keyPath in [NSArray arrayWithObjects:@"string",@"font",@"fontSize",@"foregroundColor", nil])
{
[self removeObserver:self
forKeyPath:keyPath
context:NULL];
}
#endif
[super dealloc];
}
This code will crash it.
Code:
-(IBAction) test:(id)sender
{
[myTextLayer setString:[textField stringValue]]; //works ok
[myTextLayer setFont:[NSFont fontWithName:@"Verdana"
size:48]]; //works ok
// [myTextLayer setFontSize:11]; //crash
// [myTextLayer setForegroundColor:[[NSColor blackColor] CGColor]] ;//crash
}