【问题标题】:What is causing a SIGBUS?是什么导致了 SIGBUS?
【发布时间】:2011-10-12 21:34:27
【问题描述】:

我们得到一个 SIGBUS (BUS_ADRALN),它指向这个线程。是什么导致了这个错误?第 68 行是 NSString *dateString = [dateFormat stringFromDate:currentTimestamp];或[日期格式发布];

NSDate *currentTimestamp = self.timestamp;

if (!currentTimestamp)
    return nil; // sanity check 

[currentTimestamp retain];

NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentTimestamp];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
NSDateComponents *yesterday = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[[NSDate date] addTimeInterval: -24 * 60 * 60]];

if ([today day] == [otherDay day] &&
    [today month] == [otherDay month] &&
    [today year] == [otherDay year]) {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"h:mm a"];
    NSString *dateString = [dateFormat stringFromDate:currentTimestamp];
    [dateFormat release];
    [currentTimestamp release];
    return [NSString stringWithFormat:@"Today %@", dateString];
}
else if ([yesterday day] == [otherDay day] &&
         [yesterday month] == [otherDay month] &&
         [yesterday year] == [otherDay year]) {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"h:mm a"];
    NSString *dateString = [dateFormat stringFromDate:currentTimestamp];
    [dateFormat release];
    [currentTimestamp release];
    return [NSString stringWithFormat:@"Yesterday %@", dateString];
}

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy h:mm a"];

// below is line 68
NSString *dateString = [dateFormat stringFromDate:currentTimestamp];
[dateFormat release];
[currentTimestamp release];

return dateString;

0   CoreFoundation                      0x33474894 CFDateFormatterCreateStringWithAbsoluteTime + 60
1   CoreFoundation                      0x33474817 CFDateFormatterCreateStringWithDate + 31
2   Foundation                          0x326c4ad7 -[NSDateFormatter stringForObjectValue:] + 91
3   Foundation                          0x326c4a75 -[NSDateFormatter stringFromDate:] + 21
4   MyApp                              0x000a81dc -[Message getFormattedTimestamp] (Message.m:68)
5   MyApp                              0x0007b7a4 -[ConversationView newBubble:withFrame:] (ConversationView.m:452)
6   MyApp                              0x0007b2fc -[ConversationView tableView:heightForRowAtIndexPath:] (ConversationView.m:520)
7   UIKit                               0x31bb3b09 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2173
8   UIKit                               0x31bb3225 -[UITableViewRowData numberOfRows] + 73
9   UIKit                               0x31bb2c73 -[UITableView noteNumberOfRowsChanged] + 83
10  UIKit                               0x31bb27f7 -[UITableView reloadData] + 583
11  MyApp                              0x0007cccc -[ConversationView reloadAndMoveToEnd:] (ConversationView.m:147)
12  MyApp                              0x0007cc6c -[ConversationView managedObjectContext:fetchCompletedForRequest:withResults:error:] (ConversationView.m:153)
13  MyApp                              0x00197670 -[IZManagedObjectContext backgroundFetchOperation:completedWithIDs:error:] (IZManagedObjectContext.m:150)
14  MyApp                              0x001970b0 -[IZBackgroundFetchOperation fetchRequestDidCompleteWithUserInfo:] (IZBackgroundFetchOperation.m:108)
15  CoreFoundation                      0x3347df03 -[NSObject(NSObject) performSelector:withObject:] + 23
16  Foundation                          0x327237a9 __NSThreadPerformPerform + 269
17  CoreFoundation                      0x334e7a79 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 13
18  CoreFoundation                      0x334e975f __CFRunLoopDoSources0 + 383
19  CoreFoundation                      0x334ea4eb __CFRunLoopRun + 231
20  CoreFoundation                      0x3347aec3 CFRunLoopRunSpecific + 231
21  CoreFoundation                      0x3347adcb CFRunLoopRunInMode + 59
22  GraphicsServices                    0x311a541f GSEventRunModal + 115
23  GraphicsServices                    0x311a54cb GSEventRun + 63
24  UIKit                               0x31b90d69 -[UIApplication _run] + 405
25  UIKit                               0x31b8e807 UIApplicationMain + 671
26  MyApp                              0x0009b578 main (main.m:5)

【问题讨论】:

  • 线程崩溃低于实际代码。您想查看所有线程吗?谢谢!
  • 没有其他关于崩溃的消息? `NSString *dateString = [dateFormat stringFromDate:currentTimestamp];` 的三个出现中的哪一个是第 68 行?
  • 我认为您的代码没有任何会导致崩溃的问题。
  • 将 self.timestamp 分配给 currentStamp,保留和释放而不是简单地使用 self.timestamp 的想法是什么?并且只做一次 [[[NSDateFormatter alloc] init] autorelease] 怎么样?
  • 当前时间戳是假的。我想我对尝试的事情感到绝望。我只是看不出这会如何产生这个问题。现在上面已经确定了第 68 行。基本上是最后一行。

标签: objective-c sigbus


【解决方案1】:

好吧,上面的代码很好,因为我们最终为它编写了一个单元测试。原来是内存问题。奇怪的是它总是指向这条线。

【讨论】:

  • 你能补充更多细节吗?
  • 上面的代码没问题。这实际上是导致问题的代码的完全不同部分中的内存问题。如果您看到类似的情况,我建议您使用 Instruments 来识别内存问题。至少可以说找出根本问题是痛苦的。希望这会有所帮助。
  • 我面临同样的崩溃错误:BUS_ADRALN (SIGBUS)。你能帮忙看看你是怎么找到乐器的吗?