程序崩溃,需要帮助才能找到问题至少在哪里看

时间:2011-01-02 00:08:14

标签: objective-c memory-management crash

我在崩溃前收到以下消息:

2011-01-02 00:55:15.935 XXXX[7981:207] answerButton1    
2011-01-02 00:55:15.938 XXXX[7981:207] >>>>>>>>>>>>>>>>>>>>nrQPlayer: 2
2011-01-02 00:55:15.939 XXXX[7981:207] =========whatPlayerCount===========
2011-01-02 00:55:15.939 XXXX[7981:207] ==whatPlayerCount== 1
2011-01-02 00:55:15.940 XXXX[7981:207] =========Spelare 1===========
2011-01-02 00:55:15.940 XXXX[7981:207] oooooooEND OF PLAYER!oooooooooo
2011-01-02 00:55:15.941 XXXX[7981:207] ooooooooooBEFORE IFooooooooooo
2011-01-02 00:55:15.942 XXXX[7981:207] INIT 0x5b9be30
2011-01-02 00:55:16.563 XXXX to be able to fix it[7981:207] *** -[ErrorMessage respondsToSelector:]: message sent to deallocated instance 0xca25ff0

我一直在努力追查问题的确切位置,尝试在某些地方测试'保留',但现在有些用完了。当我尝试使用断点运行调试器但它会卡住而我无法前进。

我将不胜感激任何帮助。我对此也很陌生,但这并没有使情况变得更好: - )

以下是崩溃代码的一部分:

  

案例2://两名球员

        //nrQPlayer antal spelare
        NSLog(@"=========whatPlayerCount===========");
        NSLog(@"==whatPlayerCount== %i", whatPlayerCount);
        switch (whatPlayerCount) {
            case 1:
                NSLog(@"=========Spelare 1===========");
                playerDiff = 1;
                whatPlayerCount = 2;
                thePlayer = 0;
                NSLog(@"oooooooEND OF PLAYER!oooooooooo");
                break;
            case 2:
                NSLog(@"=========Spelare 2===========");
                playerDiff = 3;
                whatPlayerCount = 1;
                thePlayer = 2;
                break;
            default:
                NSLog(@"=========break===========");
                break;
        }
        NSLog(@"ooooooooooBEFORE IFooooooooooo");
        NSLog(@"INIT %p", self);
        // >>>>>>>>HERE IS WHERE THE CRASH HAPPENS<<<<<<<<<<
        if (askedQuestions < nrOfQuestionsPerPlayer) {
            NSLog(@"1");
            if ([[finalPlayersInGame objectAtIndex:playerDiff] intValue] == 1) { // HARD
                NSLog(@"HARD 1");
                questionNr = [[hardQArray objectAtIndex:askedQuestions] intValue];
                qArray = [readQuestionFunction readQuestion: questionNr];
                question_TextView.text = [qArray objectAtIndex:0];
                NSLog(@"HARD - qNr: %i", questionNr);
            } 
            else if ([[finalPlayersInGame objectAtIndex:playerDiff] intValue] == 2) { // MEDIUM
                NSLog(@"2");
                questionNr = [[mediumQArray objectAtIndex:askedQuestions] intValue];
                qArray = [readQuestionFunction readQuestion: questionNr];
                question_TextView.text = [qArray objectAtIndex:0];
                NSLog(@"MEDIUM - qNr: %i", questionNr);
            }
            else if ([[finalPlayersInGame objectAtIndex:playerDiff] intValue] == 3) { // EASY
                NSLog(@"3");
                questionNr = [[easyQArray objectAtIndex:askedQuestions] intValue];
                qArray = [readQuestionFunction readQuestion: questionNr];
                NSLog(@"qArray: %@", qArray);
                NSLog(@"questionNr: %i", questionNr);
                question_TextView.text = [qArray objectAtIndex:0];
                NSLog(@"EASY - qNr: %i", questionNr);
            }
            NSLog(@"ooooooooooAFTER IFooooooooooo");

            NSLog(@"4");
            playerName_Label.text = [NSString stringWithFormat:@"Spelare: %@", [finalPlayersInGame objectAtIndex:thePlayer]];
            playerResult_Label.text = [NSString stringWithFormat:@"Fråga %i av %i", askedQuestions, nrOfQuestionsPerPlayer];

            //========CALL AccesQuestionDB MODULE TO SHUFFLE PLAYERS=========//
            AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];

            buttonOrder = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];

            buttonOrder = [shufflePlayersFunction shufflePlayers: buttonOrder]; // Use shufflePlayers to shuffle button also
            NSLog(@"buttonOrder: %@", buttonOrder);
            [shufflePlayersFunction release];
            NSLog(@"5");
            //========CALL buttonsOrder=========//
            ButtonOrderAccess *buttonOrderFunction = [ButtonOrderAccess new];
            [buttonOrderFunction saveButtonOrder: buttonOrder];
            [buttonOrderFunction release];

            NSLog(@"qArray: %@", qArray);
            NSLog(@"buttonOrder: %@", buttonOrder);
            [self.answerButton1 setTitle:[qArray objectAtIndex:[[buttonOrder objectAtIndex:0]intValue]] forState:UIControlStateNormal];
            [self.answerButton2 setTitle:[qArray objectAtIndex:[[buttonOrder objectAtIndex:1]intValue]] forState:UIControlStateNormal];
            [self.answerButton3 setTitle:[qArray objectAtIndex:[[buttonOrder objectAtIndex:2]intValue]] forState:UIControlStateNormal];


            if (firstQuestion == YES) {
                firstQuestion = NO;
                //secondQuestion = YES;
            }
            else {
                askedQuestions++;
                firstQuestion = YES;
            }



        }
        else {
            // Call Error Message
            ErrorMessage *callErrorMessageFunction = [ErrorMessage new];
            [callErrorMessageFunction questionError: @"Q2"];
            [callErrorMessageFunction release];
        }

3 个答案:

答案 0 :(得分:1)

使用NSZombie,一个捕获发送到解除分配对象的消息的对象,并在控制台中打印信息。

有关详细说明,请参阅此问题: How to run iPhone program with Zombies instrument?

答案 1 :(得分:0)

您要么发布不应该发布的内容,要么不保留需要保留的自动释放对象。去阅读内存管理文档。我们在这里看不到足够的代码知道它是什么。

答案 2 :(得分:0)

我们无法知道屏幕后面发生了什么,例如这里:

ErrorMessage *callErrorMessageFunction = [ErrorMessage new];
[callErrorMessageFunction questionError: @"Q2"];
[callErrorMessageFunction release];

但很可能这个ErrorMessage对象中的某些东西应该存活一段时间。

要解决此问题,请不要release,而应autorelease。返回到runloop后,对象将继续存在,这可能足以满足您的(未知)目的。

典型的顺序是:

ErrorMessage *callErrorMessageFunction = [[[ErrorMessage alloc] init] autorelease];
[callErrorMessageFunction questionError: @"Q2"];

(我更喜欢alloc + init而不是new,但它们是等效的)