drawInRect:withAttributes与“发送到释放实例的消息”
我正在更新iOS 7的应用程序。其中一个变化是切换到新的drawInRect:withAttributes函数,而不是废弃的drawInRect:withFont …这是工作正常的iOS 7testing版,但今天后升级到最新的iOS 7版本,应用程序崩溃就行了:
[text drawInRect:theRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSz], NSFontAttributeName, color, NSForegroundColorAttributeName, nil]];
随着消息:
*** -[NSStringDrawingTextStorage textContainerForAttributedString:containerSize:lineFragmentPadding:]: message sent to deallocated instance 0x187ed0f0
我尝试运行僵尸工具,这根本没有帮助,在我的代码中既没有分配也没有释放有关的对象。 具体来说,我得到的消息:
An Objective-C message was sent to a deallocated 'NSStringDrawingTextStorage' object (zombie) at address: 0x169edc50.
而对象的malloc /释放在调用者之下:
[NSStringDrawingTextStorage stringDrawingTextStorage]
我究竟做错了什么?
我能够通过从我正在渲染的NSString中修剪前导空格(包括换行符)来解决这个问题。 这是我的分类方法:
- (NSString*)stringByTrimmingLeadingWhitespace { NSUInteger index = 0; while((index < [self length]) && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember: [self characterAtIndex: index]]) { index++; } return [self substringFromIndex: index]; }
不幸的是,如果你必须保留主要的换行符,我没有其他答案。