0

抱歉...羞耻- 我没有运行调试模式,所以错误出现在错误的行上。在调试模式下,并显示正确的行,错误是一个简单的“不要访问超出其边界的数组”-错误(错别字 - 我指的是一个数组,但访问的是另一个数组)


当我从另一个线程清空 NSMutableArray 时,为什么访问 NSMutableArray 的计数会产生 SIGABRT?我认为@synchronized 应该像它的名字所暗示的那样做?

NSMutableArray *sourceImageRepArray;

...

@synchronized(self)
{
   NSLog(@"singleton 1 %p", self);
   unsigned long count = sourceImageRepArray.count; //SIGABRT here!
   ...

不知道你想让我分享多少额外的代码......这是触发它的代码:

@synchronized([Singleton sharedSingleton])
{
   NSLog(@"singleton 2 %p", [Singleton sharedSingleton]);
   int i, n;
   n = [filenames count];
   for(i=0; i<n; i++){
      ImageRep *item = [[ImageRep alloc] init];
      [item setPath:[filenames objectAtIndex:i]];   
      [[Singleton sharedSingleton].targetImageRepArray insertObject:item atIndex: [targetImageBrowser indexAtLocationOfDroppedItem]];
      [item release];
      [[Singleton sharedSingleton] removeImageRepFromArray:[Singleton sharedSingleton].sourceImageRepArray withPath:[filenames objectAtIndex:i]];
   }
}

单例 1 == 单例 2:

2011-02-08 07:22:03.265 Crash[60001:5d03] singleton 1 0x200047680
2011-02-08 07:22:03.433 Crash[60001:a0f] singleton 2 0x200047680
  • 为什么不同步?!还有什么可能发生的?
4

2 回答 2

0

你确定你的 self 和 [Singleton sharedSingleton] 是同一个对象吗?

于 2011-02-08T02:53:01.060 回答
0

抱歉...羞耻- 我没有运行调试模式,所以错误出现在错误的行上。在调试模式下,并显示正确的行,错误是一个简单的“不要访问超出其边界的数组”-错误(错别字 - 我指的是一个数组,但访问的是另一个数组)

所以我的问题的解决方案是:

不要在发布时进行 NSLog 调试,它可能会在错误的行上标记错误。(有时,在多线程应用程序中,我猜?)

于 2011-02-08T18:03:20.807 回答