drawInRect:withAttributes 因“消息发送到已释放的实例"而消失; [英] drawInRect:withAttributes dies with "message sent to deallocated instance"
问题描述
我正在为 iOS 7 更新应用程序.其中一项更改是切换到新的 drawInRect:withAttributes 函数,而不是已弃用的 drawInRect:withFont...这在 iOS 7 测试版上运行良好,但今天升级到最新的 iOS 7 版本后,应用程序崩溃了:
I am updating an app for iOS 7. One of the changes is switching to the new drawInRect:withAttributes function instead of the deprecated drawInRect:withFont... This was working fine on the iOS 7 beta, but today after upgrading to the latest iOS 7 version, the app crashes on the line:
[text drawInRect:theRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSz], NSFontAttributeName, color, NSForegroundColorAttributeName, nil]];
随着消息:
*** -[NSStringDrawingTextStorage textContainerForAttributedString:containerSize:lineFragmentPadding:]: message sent to deallocated instance 0x187ed0f0
我尝试运行 Zombie 工具,这在我的代码中根本没有帮助,无论是分配还是释放有问题的对象.具体来说,我收到了消息:
I tried running the Zombie instrument, which is not helpful at all neither the allocation nor the release of the object in question are in my code. Specifically I get the message:
An Objective-C message was sent to a deallocated 'NSStringDrawingTextStorage' object (zombie) at address: 0x169edc50.
并且对象的 malloc/release 在调用者之下:
And the malloc/release of the object are under the caller:
[NSStringDrawingTextStorage stringDrawingTextStorage]
我做错了什么?
推荐答案
我能够通过从我渲染的 NSString 中修剪前导空格(包括换行符)来解决这个问题.这是我的分类方法:
I was able to work around this by trimming the leading whitespace (including newline characters) from the NSString I was rendering. Here's my category method:
- (NSString*)stringByTrimmingLeadingWhitespace
{
NSUInteger index = 0;
while((index < [self length]) && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember: [self characterAtIndex: index]])
{
index++;
}
return [self substringFromIndex: index];
}
不幸的是,如果您必须保留前导换行符,我没有其他答案.
Unfortunately if you must preserve the leading newline character(s), I do not have an alternative answer.
这篇关于drawInRect:withAttributes 因“消息发送到已释放的实例"而消失;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!