NSMutableArray在多线程应用程序中运行SIGABRT ...但我_am_做@synchronized!

时间:2011-02-07 23:06:54

标签: multithreading cocoa nsmutablearray synchronized sigabrt

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


当我从另一个线程清空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]];
   }
}

singleton 1 == singleton 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
  • 为什么不同步?!还有什么可以继续?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

所以问题的解决方案是:

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