1

我正在为 iOS 7 更新应用程序。其中一项更改是切换到新的 drawInRect:withAttributes 函数而不是已弃用的 drawInRect:withFont... 这在 iOS 7 测试版上运行良好,但今天在升级到最新的 iOS 之后7版本,app崩溃上线:

[text drawInRect:theRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSz], NSFontAttributeName, color, NSForegroundColorAttributeName, nil]];

随着消息:

*** -[NSStringDrawingTextStorage textContainerForAttributedString:containerSize:lineFragmentPadding:]: message sent to deallocated instance 0x187ed0f0

我尝试运行 Zombie 工具,这在我的代码中既没有分配也没有释放有问题的对象,这根本没有帮助。具体来说,我收到消息:

An Objective-C message was sent to a deallocated 'NSStringDrawingTextStorage' object (zombie) at address: 0x169edc50.

并且对象的 malloc/release 在调用者之下:

[NSStringDrawingTextStorage stringDrawingTextStorage]

我究竟做错了什么?

4

1 回答 1

1

我可以通过修剪我正在渲染的 NSString 中的前导空格(包括换行符)来解决这个问题。这是我的分类方法:

- (NSString*)stringByTrimmingLeadingWhitespace
{
    NSUInteger index = 0;

    while((index < [self length]) && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember: [self characterAtIndex: index]])
    {
        index++;
    }

    return [self substringFromIndex: index];
}

不幸的是,如果您必须保留前导换行符,我没有其他答案。

于 2013-10-16T15:13:05.163 回答