在NSArray上保留属性和EXC_BAD_ACCESS

时间:2013-06-04 00:44:49

标签: objective-c exc-bad-access retain

我已经浏览了EXC_BAD_ACCESS上的所有相关帖子,但未能明确这个概念。也许我应该首先学习基本概念,但现在我需要解决这个问题。所以,介入以获得一些专家帮助..

这是我的代码(我只粘贴了相关部分)

//In .h
//=====
        @interface GUIMainController : GUIController {
        ::
        ::
            NSArray* shufflePositionArray;
        }

        @property (retain)NSArray* shufflePositionArray;

//In .mm
//=======
        @synthesize shufflePositionArray;

        -(void)start
        {
        ::
            NSString* shufflePositionArrayPlistPath=nil;
            NSString* shufflePositionArrayPlistPathFromConfiguration=
            @"/MyApp/Configuration/ShufflePositionArray.plist";
           if ([[NSFileMngrdfltMngr]fileExistsAtPath:shufflePositionArrayPlistPathFromConfig])
            {
             self.shufflePositionArray=  
                     [NSArrayarrayWithContentsOfFile:shufflePositionArrayPlistPathFromConfig];
            }
            if (self.shufflePositionArray==nil)  {
           shufflePositionArrayPlistPath=
           [thisBundle pathForResource:@"ShufflePositionArray" ofType:@"plist"];

             if (shufflePositionArrayPlistPath!=nil) {
                    self.shufflePositionArray=
                          [NSArray arrayWithContentsOfFile:shufflePositionArrayPlistPath];
                }
            }
        }

//From another method:
//====================

        -(void)shuffleWindow
        {
        ::
            if (shufflePositionArray!=nil && [self.shufflePositionArray count]!=0){
        :: // some code
            if (shufflePositionArray!=nil && [self.shufflePositionArray count]!=0){
        ::
        ::  // some code
            processwindowlocation_=(processwindowlocation_+1)%[self.shufflePositionArray count];    
                 // ######## there is a crash here !!!!!!   #############
            }
         }

        }
//And then..we release it here:
//==============================
        -(void)dealloc {

            if(shufflePositionArray!=nil){
                [shufflePositionArray release];
            }
            [super dealloc];
        }

我是obj-C的新手,任何形式的帮助都会受到赞赏。 提前谢谢。

这是崩溃:

Crashed Thread:0 Dispatch queue:com.apple.main-thread

异常类型:EXC_BAD_ACCESS(SIGSEGV) 例外代码:KERN_INVALID_ADDRESS位于0x0000000000000018

VM区域接近0x18: - >     __TEXT 000000010d89a000-000000010d927000 [564K] r-x / rwx SM = COW /Ihome/MyAppCE.app/Contents/MacOS/MyAppCE

特定应用信息: objc_msgSend()选择器名称:count

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x00007fff82c5d62f objc_msgSend_vtable9 + 47
1   com.apple.AppKit                0x00007fff88842b05 +[NSScreen screens] + 847
2   com.apple.AppKit                0x00007fff88844575 +[NSScreen _zeroScreenHeight] + 44
3   com.apple.AppKit                0x00007fff888b15cf _NSShapeRoundedWindowWithWeighting + 61
4   com.apple.AppKit                0x00007fff888507c3 -[NSThemeFrame shapeWindow] + 336
5   com.apple.AppKit                0x00007fff888b055c -[NSThemeFrame setFrameSize:] + 457
6   com.apple.AppKit                0x00007fff888af68f -[NSWindow _setFrame:updateBorderViewSize:] + 929
7   com.apple.AppKit                0x00007fff888aee04 -[NSWindow _oldPlaceWindow:] + 1142
8   com.apple.AppKit                0x00007fff888ae2b9 -[NSWindow _setFrameCommon:display:stashSize:] + 1837
9   com.Apple.Phoenix.UI            0x0000000110580045 -[GUIMainController shuffleWindow] + 1477 (GUIMainController.mm:470)
10  com.Apple.Phoenix.UI            0x0000000110586aef -[GUIPeriodicController periodic:] + 2399 (GUIPeriodicController.mm:219)
11  com.apple.Foundation            0x00007fff86867463 __NSFireTimer + 96
12  com.apple.CoreFoundation        0x00007fff81df5804 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
13  com.apple.CoreFoundation        0x00007fff81df531d __CFRunLoopDoTimer + 557
14  com.apple.CoreFoundation        0x00007fff81ddaad9 __CFRunLoopRun + 1529
15  com.apple.CoreFoundation        0x00007fff81dda0e2 CFRunLoopRunSpecific + 290
16  com.apple.HIToolbox             0x00007fff83dc6eb4 RunCurrentEventLoopInMode + 209
17  com.apple.HIToolbox             0x00007fff83dc6b94 ReceiveNextEventCommon + 166
18  com.apple.HIToolbox             0x00007fff83dc6ae3 BlockUntilNextEventMatchingListInMode + 62
19  com.apple.AppKit                0x00007fff8887f533 _DPSNextEvent + 685
20  com.apple.AppKit                0x00007fff8887edf2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
21  com.Apple.Phoenix.UI            0x0000000110594b52 processUIEvent() + 290 (PhoenixCECocoaGUI.mm:363)
22  com.Apple.Phoenix.UI            0x000000011058e152 Phoenix::Core::UICocoaImpl::processEvent() + 44 (UICocoaImpl.mm:61)
23  com.Apple.MyAppCE               0x000000010d8ae62e main + 2302 (mainer.cpp:180)
24  com.Apple.MyAppCE               0x000000010d89b604 start + 52


Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x000000000000000f  rbx: 0xffffffffffffffff  rcx: 0x0000000001010101  rdx: 0x0000000000000001
  rdi: 0xffffffffffffffff  rsi: 0x00007fff7268ea80  rbp: 0x00007fff523631b0  rsp: 0x00007fff52363028
   r8: 0x00007fff72435a30   r9: 0x00007fff72435a01  r10: 0x0000000000000000  r11: 0x00007f968c000600
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000000001  r15: 0x000000000000000c
  rip: 0x00007fff82c5d62f  rfl: 0x0000000000010206  cr2: 0x0000000000000018

1 个答案:

答案 0 :(得分:1)

如果发生了崩溃,请发布回溯。

假设您没有删除一些相关代码,processwindowlocation_=(processwindowlocation_+1)%[self.shufflePositionArray count];可能崩溃的唯一方法(假设这实际上是崩溃的行)是self无效或{ {1}}无效。

请注意,无效并不代表shufflePositionArray; nil只会返回nil来计算。

另请注意,此处检查非零是毫无意义的:

0

那次崩溃很奇怪。考虑到你的代码,我真的不希望崩溃。

尝试打开Zombie检测(乐器 - Google可以根据需要找到更多信息)并查看是否是过度发布问题。