NSString sizeWithFont上的EXC_BAD_ACCESS

时间:2012-10-02 04:45:55

标签: objective-c ios nsstring exc-bad-access iphone-5

我不知道发生了什么事。我已经尝试了很多东西让我弄清楚在这个导致EXC_BAD_ACCESS的特定代码行上发生了什么。 我试过启用NSZombies,但它没有帮助我。这是代码:

- (int)linesFromText:(NSString *)string withFont:(UIFont *)font andSize:(CGSize)size {
    NSArray *splitString = [string componentsSeparatedByString:@" "];
    NSMutableArray *allLines = [NSMutableArray array];
    NSMutableString *line = [NSMutableString string];
    NSString *word;
    NSString *fakeLine;
    for (int i = 0; i < splitString.count; i++) {

        word = [splitString objectAtIndex:i];
        fakeLine =  [NSString stringWithFormat:@"%@%@ ",line, word];
        //NSLog(@"line %@, font %@",fakeLine,font);

        ////THIS IS THE LINE CAUSING THE EXC_BAD_ACCESS
        CGSize lineSize = [fakeLine sizeWithFont:font];

        if (lineSize.width <= size.width) {
            [line appendFormat:@"%@ ", word];
        } else {
            [allLines addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
            line = [NSMutableString string];
            [line appendFormat:@"%@ ", word];
        }


    }
    [allLines addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
    return allLines.count;
}

这让我疯狂,因为当你在我建立的应用程序中滚动快节目时,它只会发生在新的iphone 5上。以下是商店中应用程序的链接:

http://itunes.apple.com/us/app/id543324451?mt=8

如果你有iphone 5,你可以看到我的意思。 iphone 4没有这样做。

此代码在UITableViewCell的layoutSubviews中调用,有助于调整使用TTTAttributedLabel(https://github.com/mattt/TTTAttributedLabel)的自定义归属标签的框架。

我也尝试启用“Gaurd malloc尝试给我更多详细信息,但我的XCode给了我这个错误:

dyld:无法加载插入的库'/usr/lib/libgmalloc.dylib'因为找不到图像

如果我在/ usr / lib中查找该文件是指向同一目录中存在的文件的sym链接: libgmalloc.dylib - &gt; libgmalloc.B.dylib

我的想法已经用完了,并且认为它可能是UIFont很快就会被释放然后它不可用所以我在UITableViewCell中引用了UIFont,直到Cell的生命结束。

我也在互联网上搜索过,并没有在这些细节上找到太多。

此处还有来自调试器的堆栈跟踪图像:

http://i.stack.imgur.com/gWC5L.png

有什么想法吗? 我提供了足够的信息吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我认为你的问题在这里得到了回答:

UIStringDrawing methods don't seem to be thread safe in iOS 6

短版本:sizeWithFont以及大多数其他UIKit方法,当您在屏幕上使用它时,它不是线程安全的(而不是使用它来预渲染)。

好消息:看看Adam Swinden在那个帖子中的答案;他解释了如何使用CoreText而不是UIKit在iOS6中获得相同的结果。