Reputation: 1953
I've some code to setup an NSMutableAttributedString:
NSMutableParagraphStyle *centred = [[NSMutableParagraphStyle alloc]init];
[centred setAlignment:NSCenterTextAlignment];
NSDictionary *attributes = [[NSDictionary alloc]initWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica bold" size:12],NSFontAttributeName,
[NSColor whiteColor],NSForegroundColorAttributeName,
[NSColor clearColor], NSBackgroundColorAttributeName,
centred, NSParagraphStyleAttributeName,
nil];
NSMutableAttributedString* attribTitle = [[NSMutableAttributedString alloc]initWithString:@"Foo"];
[attribTitle setAttributes:attributes range:NSMakeRange(0, [[attribTitle string] length] - 1)];
It's been sitting in my app's -awakeFromNib method almost since I started the project with no problems but just today I've begun to get every other attempt at running the app in Xcode 4.3 crashing out with "Thread 1:EXC_BAD_ACCESS (code=1, address=0x10)". It breaks on the final '...setAttributes...' line. At one point I tried using the method – initWithString:attributes: to initialise attribTitle and sure enough the crash (when it did) was on the alloc-init line.
I've tried clearing Xcode restarting but it still happens about every other build... If the app doesn't crash then the attrbibuted string appears as expected.
I'm wondering if I have a corrupted project file, but just in case I've done something daft, can anyone point out a mistake - presumably it's in the 'attributes' NSDictionary?
Thanks!
Todd.
Upvotes: 3
Views: 877
Reputation: 1464
Is this the code you run, of is it simplified? As it is, it looks Ok to me (I dare say). When it crashes only every other time, than that indicates some other circumstance, which is not visible in the code here.
You mention that this is in -awakeFromNib:
— is it possible that this method runs twice? I had that a number of times confusing me, and it happens quite easily when e.g. you create a window controller as an IB object in MainMenu.xib, and then have said window controller load a NIB via -[super initWithWindowNibName:]
in its own -init:
. This calls -awakeFromNib
once for each involved NIB. I had created some order of initialization issue in my case, which showed similar undetermined behavior to what you describe.
I moved the code to -windowWillLoad
and -windowDidLoad
method, which is per window and therefore doesn't have this problem.
Upvotes: 1