从不同线程使用 NSDateFormatter 时会崩溃

发布于 2024-10-16 11:54:35 字数 580 浏览 7 评论 0原文

我们不断遇到 NSDateFormatter 随机、奇怪的崩溃。相关的堆栈跟踪是:

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:] ()

日期格式化程序仍在内存中(即未释放或损坏)。我唯一能想到的是崩溃时的字符串不符合格式,但我怀疑这会让格式化程序完全崩溃。 (事先检查格式并非易事)。

有什么想法吗?

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).

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

橙幽之幻 2024-10-23 11:54:35

感谢前面几位的回答。

这不是内存问题。结果发现是同步问题。 NSDateFormatter 不是线程安全的;有一个后台线程试图同时使用相同的格式化程序(因此具有随机性)。

希望这对将来的人有帮助!

Thanks to the previous answerers.

This was not a memory problem. It turned out to be a synchronization issue. NSDateFormatters 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!

葮薆情 2024-10-23 11:54:35

另一种解决方案是序列化使用 NSDateFormatter 或任何其他非线程安全对象的代码的执行。使用 Grand Central Dispatch,您可以将代码推送到 main_queue:

dispatch_async(dispatch_get_main_queue(), ^(void){
  [some_object some_message];
});

或使用私有队列来达到相同的效果:

dispatch_queue_t dispatch_queue = dispatch_queue_create("com.MyApp.serializer",NULL);
dispatch_async(dispatch_queue, ^(void){
  [some_object some_message];
});

Another solution would be to serialize the execution of the code that uses NSDateFormatters, or any other non-thread-safe objects. Using Grand Central Dispatch you can push the code on the main_queue:

dispatch_async(dispatch_get_main_queue(), ^(void){
  [some_object some_message];
});

or use a private queue to achieve the same effect:

dispatch_queue_t dispatch_queue = dispatch_queue_create("com.MyApp.serializer",NULL);
dispatch_async(dispatch_queue, ^(void){
  [some_object some_message];
});
兲鉂ぱ嘚淚 2024-10-23 11:54:35

当您使用任何已释放的对象时,将会发生 EXCBADAACCESS...
尝试使用 NSZombie .. 这是查找 EXCBADAACCESS 发生位置的简单方法...它将指定哪个方法以及哪个对象被释放

请参阅此链接 http://www.markj.net/iphone-memory-debug-nszombie/

EXCBADACCESS will occur when you use any deallocated object...
Try to use NSZombie.. It is a easy way to find where the EXCBADACCESS occurs... It will specify which Method where and which object gets deallocated

See this Link http://www.markj.net/iphone-memory-debug-nszombie/

烦人精 2024-10-23 11:54:35

我敢打赌,您传递给日期格式化程序的字符串已被过度释放。

My bet is that the string you pass in to the date formatter is over-released.

扛刀软妹 2024-10-23 11:54:35

我遇到了 _sigtramp 的奇怪崩溃,这导致应用程序看起来被锁定,但仍在屏幕上 - 完全阻碍了真正的根本原因。

事实证明,我们确实引入了多线程数据解析,这与尝试使用 NSDateFormatter 解析日期的主 GUI 线程发生了冲突。

对 NSDateFormatter formatDate 调用进行一些同步解决了问题。

I was experiencing weird crashes with _sigtramp which caused to application to appear locked up but still on the screen - completely obstructing the real root cause.

It turned out indeed that we introduced multi-thread data parsing which collided with the main GUI thread trying to parse dates using NSDateFormatter.

Putting some synchronization around the NSDateFormatter formatDate calls resolved the issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文