在两个线程上同时使用任何UIStringDrawing方法都会导致崩溃.我的理解是所有UIStringDrawing方法都是iOS 4.0中的线程安全的.
这段代码(没有任何用处)演示了这个问题:
dispatch_queue_t queue = dispatch_queue_create("com.queue", NULL);
for (int i = 0; i < 10000; i++) {
dispatch_async(queue, ^{
NSString *string = @"My string";
CGSize size = [string sizeWithFont:[UIFont boldSystemFontOfSize:13]];
});
}
for (int i = 0; i < 10000; i++) {
NSString *string = @"My string";
CGSize size = [string sizeWithFont:[UIFont boldSystemFontOfSize:13]];
}
dispatch_release(queue);
Run Code Online (Sandbox Code Playgroud)
应用程序在循环几次迭代后崩溃,并带有以下回溯:
* thread #1: tid = 0x2403, 0x00ad40c8, stop reason = EXC_BAD_ACCESS (code=2, address=0xad40c8)
frame #0: 0x00ad40c8
frame #1: 0x36bc4252 WebCore`WebCore::Font::Font(WebCore::FontPlatformData const&, WTF::PassRefPtr<WebCore::FontSelector>) + …Run Code Online (Sandbox Code Playgroud)