问题描述
我们一直在用 NSDateFormatter
获得一个随机的,奇怪的崩溃。相关的堆栈跟踪是:
We keep getting a random, weird crash with NSDateFormatter
. The relevant stack trace is:
Program received signal: "EXC_BAD_ACCESS".
#0 0x00000005 in ?? ()
#1 0x0213e3c3 in udat_parse ()
#2 0x01d4e1ca in CFDateFormatterGetAbsoluteTimeFromString ()
#3 0x01d4e225 in CFDateFormatterCreateDateFromString ()
#4 0x003e2608 in getObjectValue ()
#5 0x003e2921 in -[NSDateFormatter getObjectValue:forString:errorDescription:] ()
#6 0x003e21cd in -[NSDateFormatter dateFromString:] ()
日期格式化程序仍在内存中(即未释放或损坏)。我唯一能想到的是崩溃时的字符串不符合格式,但我怀疑这会使格式化程序完全崩溃。 (预先检查格式是非常简单的。)
The date formatter is still in memory (i.e. not released or corrupted). The only thing I can think of is the strings upon crash do not conform to the format, but i doubt that will make the formatter completely crash. (it is non trivial to check the format beforehand).
有什么想法吗?
推荐答案
感谢之前的回答者。
这不是内存问题。结果证明是同步问题。 NSDateFormatter
s不是线程安全的;有一个后台线程试图同时使用相同的格式化程序(因此随机性)。
This was not a memory problem. It turned out to be a synchronization issue. NSDateFormatter
s are not thread safe; there was a background thread attempting to use the same formatter at the same time (hence the randomness).
希望这有助于将来的某个人!
Hope this helps someone in the future!
这篇关于从不同的线程使用时,NSDateFormatter崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!