0

我想將日誌寫入全局默認隊列中的文件。它在iOS 6中運行良好,但即使塊爲空也會在iOS 5中崩潰。GCD dispatch_async DISPATCH_QUEUE_PRIORITY_DEFAULT EXC_BAD_ACCESS崩潰

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
//fprintf(s_logfp, "[%s] %s\n", [[[NSDate date] hipuClientLogDescription] UTF8String], [log UTF8String]); 
}); 

它去所有,如果合適的我的代碼是這樣的:

logQueue = dispatch_queue_create("com.hipu.clientlog", NULL); 
dispatch_async(logQueue, ^{ 
    fprintf(s_logfp, "[%s] %s\n", [[[NSDate date] hipuClientLogDescription] UTF8String], [log UTF8String]); 
}); 

調用堆棧:

#0 0x01dff0b0 in objc_msgSend() 
#1 0x01dffd40 in objc_retain() 
#2 0x0044a712 in HpWriteLogToDisk at /Users/tanqiyu/Documents/hipu_work/iOS/trunk/HipuClient/HpFoundation/HpFoundation/HpLog.m:60 
#3 0x003420b6 in -[HpEngine initWithRootDir:appVersion:] at /Users/tanqiyu/Documents/hipu_work/iOS/trunk/HipuClient/HpEngine/HpEngine/Engine/HpEngine.m:69 
#4 0x00341a8d in +[HpEngine initEngineWithRootDir:appVersion:] at /Users/tanqiyu/Documents/hipu_work/iOS/trunk/HipuClient/HpEngine/HpEngine/Engine/HpEngine.m:28 
#5 0x0000abcd in -[AppDelegate application:didFinishLaunchingWithOptions:] at /Users/tanqiyu/Documents/hipu_work/iOS/trunk/HipuClient/HipuClient/HipuClient/AppDelegate.m:35 
#6 0x00f5a9d6 in -[UIApplication _callInitializationDelegatesForURL:payload:suspended:]() 
#7 0x00f5b8a6 in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:]() 
#8 0x00f6a743 in -[UIApplication handleEvent:withNewEvent:]() 
#9 0x00f6b1f8 in -[UIApplication sendEvent:]() 
#10 0x00f5eaa9 in _UIApplicationHandleEvent() 
#11 0x02325fa9 in PurpleEventCallback() 
#12 0x021661c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__() 
#13 0x020cb022 in __CFRunLoopDoSource1() 
#14 0x020c990a in __CFRunLoopRun() 
#15 0x020c8db4 in CFRunLoopRunSpecific() 
#16 0x020c8ccb in CFRunLoopRunInMode() 
#17 0x00f5b2a7 in -[UIApplication _run]() 
#18 0x00f5ca9b in UIApplicationMain() 
#19 0x0000a97d in main at 
+2

發佈崩潰的堆棧跟蹤。 – 2013-05-03 03:39:10

+0

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {});爲我工作 – 2013-05-03 03:41:09

+0

rob-mayoff,我已經發布了調用堆棧,謝謝。 – qiyu 2013-05-03 04:58:20

回答

0

如果它在你一定破壞內存塊沒有代碼崩潰。

當你解決這個問題:

1)確保slogfp是一個全球性的,並不會改變。

2)如果string是零,你會崩潰試圖使用一個char *是零。建議您先創建一個NSString,然後測試其有效性,然後將其分派給日誌記錄。