gpt4 book ai didi

objective-c - NSString sizeWithFont 上的 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-12-01 16:54:42 25 4
gpt4 key购买 nike

我不知道发生了什么。我已经尝试了很多方法来弄清楚导致 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 该文件是指向同一目录中确实存在的文件的符号链接(symbolic link):
libgmalloc.dylib -> libgmalloc.B.dylib

我在这里没有想法,并认为它可能是 UIFont 很快就会发布然后它不可用,所以我在 UITableViewCell 中放置了引用来保存 UIFont 直到该 Cell 的生命结束。

我也在互联网上搜索过,并没有找到太多关于这个细节的东西。

这里还有一张来自调试器的堆栈跟踪图像:

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

有任何想法吗?
我是否提供了足够的信息?

谢谢

最佳答案

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

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

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

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

关于objective-c - NSString sizeWithFont 上的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com