簡體   English   中英

-[CFString保留]:消息發送到已釋放實例0x215076c0

[英]-[CFString retain]: message sent to deallocated instance 0x215076c0

我知道這個主題已經被討論過幾次了,但是我無法弄清楚是什么導致了此錯誤消息。 我已經在這里停留了兩個星期,這讓我發瘋。 方法的第一部分為2-4個玩家設置了比賽。 找到比賽后,它將嘗試在特定限制內添加3和4個玩家。 目前,該限制僅設置為3秒,但是我認為一旦准備發布,該限制將會更大。

我知道這是matchmakerViewController代碼中某處的內存錯誤。 當我注釋掉更改場景的代碼時,仍然出現錯誤。 您能告訴我我做錯了什么以及如何解決嗎?

-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{
    NSLog(@"IN matchmakerViewController!");

    NSLog(@"Found a match!");
    //startDate = [[NSDate date] retain];

    //NSArray * playerIds1 = match.playerIDs;
    //[marray_players addObject:playerIds1];

    // Setup match
    TXGameCenterManager *gameCenterManager = [TXGameCenterManager sharedTXGameCenterManager];
    gameCenterManager.multiplayerMatch = match;
    // The delegate of the match is HelloWorldLayer
    gameCenterManager.multiplayerMatch.delegate = self;
    AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
    [delegate.viewController dismissModalViewControllerAnimated:NO];


    GKMatchRequest * matchRequest = [[[GKMatchRequest alloc] init] autorelease];


    NSLog(@"playerIDs count is %d",[match.playerIDs count] );


    if( match.expectedPlayerCount==0 )
    {
        // Launching the game without waiting for connection change messages
        NSLog(@"Begin game without waiting for match connection change messages");
        // Determine the host, local or remote
        NSArray * playerIds = match.playerIDs;
        NSLog(@"Number of players: %d", [playerIds count]);
        NSLog(@"ID of player: %@", [playerIds lastObject]);
        NSLog(@"I got the player ids");


        [GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError * error)
         {

             NSLog(@"Player 1 %@", [[players objectAtIndex:0] alias] );
            // NSLog(@"Player 2 %@", [[players objectAtIndex:1] alias] );


            if( !error) 
                {
                NSLog(@"Local player: %@", [[GKLocalPlayer localPlayer] alias]);
                NSLog(@"Remote player: %@", [[players lastObject] alias]);
                NSLog(@"Remote player: %@", [[players lastObject] alias]);

                //NSArray *nsarray_player2 = [marray_players objectAtIndex:1];

               // remote1name= [[nsarray_player1 lastObject] alias];

                int_totalremoteplayers=[match.playerIDs count];
                boo_playersfound=true;


                localName = [[GKLocalPlayer localPlayer] alias];
                remoteName = [[players objectAtIndex:0] alias];

                NSLog(@"Player 1 %@", [[players objectAtIndex:0] alias] );
              //  NSLog(@"Player 2 %@", [[players objectAtIndex:1] alias] );


                //remote1name= [[nsarray_player1 lastObject] alias];

                if ([match.playerIDs count]==1)
                {
                    NSLog(@"IN [match.playerIDs count]==1");

                    remoteplayer1name= [[players objectAtIndex:0] alias];
                    [playernames addObject:remoteplayer1name];
                    [playernames addObject:localName];

                    NSArray *sortedArray;
                    sortedArray = [playernames sortedArrayUsingSelector:
                                   @selector(localizedCaseInsensitiveCompare:)];

                    NSLog(@"Host: %@", [sortedArray objectAtIndex:0]);
                    NSLog(@"Player1: %@", [sortedArray objectAtIndex:1]);


                    hostName=[sortedArray objectAtIndex:0];
                    remoteplayer1name=[sortedArray objectAtIndex:1];

                    if ([hostName isEqualToString:localName])
                    {
                        [[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
                    }

                }

                if ([match.playerIDs count]==2)
                {
                    NSLog(@"IN [match.playerIDs count]==2");
                    NSLog(@" [match.playerIDs count]==2 Player 1 %@", [[players objectAtIndex:0] alias] );
                    NSLog(@"[match.playerIDs count]==2 Player 2 %@", [[players objectAtIndex:1] alias] );
                    NSLog(@"[match.playerIDs count]==2 localName %@", localName );

                    remoteplayer1name= [[players objectAtIndex:0] alias];
                    remoteplayer2name= [[players objectAtIndex:1] alias];
                    [playernames addObject:remoteplayer1name];
                    [playernames addObject:remoteplayer2name];
                    [playernames addObject:localName];


                     NSLog(@"Players in playernames %d", [playernames count]);


                    NSArray *sortedArray;
                    sortedArray = [playernames sortedArrayUsingSelector:
                                   @selector(localizedCaseInsensitiveCompare:)];

                    NSLog(@"Host: %@", [sortedArray objectAtIndex:0]);
                    NSLog(@"Player1: %@", [sortedArray objectAtIndex:1]);
                    NSLog(@"Player2: %@", [sortedArray objectAtIndex:2]);


                    hostName=[sortedArray objectAtIndex:0];
                    remoteplayer1name=[sortedArray objectAtIndex:1];
                    remoteplayer2name=[sortedArray objectAtIndex:2];

                    if ([hostName isEqualToString:localName])
                    {
                        [[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
                    }

                     NSLog(@"match.expectedPlayerCount = %d",match.expectedPlayerCount);


                }

                else if ([match.playerIDs count]==3)
                {
                    remoteplayer1name= [[players objectAtIndex:0] alias];
                    remoteplayer2name= [[players objectAtIndex:1] alias];
                    remoteplayer3name= [[players objectAtIndex:2] alias];
                    [playernames addObject:remoteplayer1name];
                    [playernames addObject:remoteplayer2name];
                    [playernames addObject:remoteplayer3name];
                    [playernames addObject:localName];

                    NSArray *sortedArray;
                    sortedArray = [playernames sortedArrayUsingSelector:
                                   @selector(localizedCaseInsensitiveCompare:)];

                    NSLog(@"Host: %@", [sortedArray objectAtIndex:0]);
                    NSLog(@"Player 1: %@", [sortedArray objectAtIndex:1]);
                    NSLog(@"Player 2: %@", [sortedArray objectAtIndex:2]);
                    NSLog(@"Player 3: %@", [sortedArray objectAtIndex:3]);


                    hostName=[sortedArray objectAtIndex:0];
                    remoteplayer1name=[sortedArray objectAtIndex:1];
                    remoteplayer2name=[sortedArray objectAtIndex:2];
                    remoteplayer3name=[sortedArray objectAtIndex:3];

                    if ([hostName isEqualToString:localName])
                    {
                        [[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
                    }
                }


                NSLog(@"Player names");
                // TODO: Set the correct player names in the globals
                player1 = localName;
                player2 = [[NSUserDefaults standardUserDefaults]  objectForKey:@"player2name"];
                player3 = [[NSUserDefaults standardUserDefaults]  objectForKey:@"player3name"];
                player4 = [[NSUserDefaults standardUserDefaults]  objectForKey:@"player4name"];


                NSLog(@"Launch the game");
                // Launch the game
                int_team2tally=0;
                NSLog(@"boo_startgame");
                boo_startgame=true;
                NSLog(@"go to the required scene");
                //go to the required scene

            }



            else
            {
                NSLog(@"Error");
            }

             NSLog(@"StartMultiplayerGameTablehost");
             //gameCenterManager.matchController.matchmakerDelegate = nil;
             [self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:1.];

        }


         ];

     NSLog(@"No Error");

    }

// Believe the error is somewhere in the following code.
    if (boo_gamestarted!=true)
    {

        if (match.expectedPlayerCount==0)
        {
           //if([localName isEqualToString:hostName])
           // {
                boo_startgame=true;

                if ([match.playerIDs count]==1)
                {

                matchRequest.minPlayers = 3;
                matchRequest.maxPlayers = 4;
                matchRequest.playersToInvite = nil;

                [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
                 {
                     if (error)
                     {
                         NSLog(@"An error occurred during adding a player to match: %@", [error localizedDescription]);}
                     else if (match != nil)
                     {NSLog(@"A player has been added to the match");}
                 }];
                }

                else if ([match.playerIDs count]==2)
                {
                    matchRequest.minPlayers = 4;
                    matchRequest.maxPlayers = 4;
                    matchRequest.playersToInvite = nil;

                    [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
                     {
                         if (error)
                         {
                             NSLog(@"An error occurred during adding a player to match: %@", [error localizedDescription]);}
                         else if (match != nil)
                         {NSLog(@"A player has been added to the match");}
                     }];
                }


            if ([match.playerIDs count]==3)
            {
                 NSLog(@"Starting Game-589");
                // [CCTimer timerWithTarget:self selector:@selector(StartMultiplayerGameTablehost) interval:10];
                //[self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay: 1.];

            }  // end if (match.expectedPlayerCount==0)

            else if (boo_gamestarted==false)
            {
                NSLog(@"player count = %d",[match.playerIDs count]);
                NSLog(@"Starting Game-597");
               // [self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:1.];

            }


                //[CCTimer timerWithTarget:self selector:@selector(StartMultiplayerGameTablehost) interval:10];
               // [self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:3.];
           // }

            //
            else
            {

                boo_startgame=true;


                matchRequest.minPlayers = 3;
                matchRequest.maxPlayers = 4;
                matchRequest.playersToInvite = nil;

                [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
                 {
                     if (error)
                     {
                         NSLog(@"An error occurrred during adding a player to match: %@", [error localizedDescription]);}
                     else if (match != nil)
                     {NSLog(@"A player has been added to the match");}
                 }];
             [self performSelector:@selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:3.];



            }      
        }
    }    
}


-(void)StartMultiplayerGameTablehost
{
    boo_gamestarted=true;

  [[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}

-(void)dealloc
{
    [super dealloc];
}

最終結果是一條消息,看起來像:

#0  0x32499468 in ___forwarding___ ()
#1  0x323f0f68 in __forwarding_prep_0___ ()
#2  0x31ffac5e in _Block_object_assign ()
#3  0x39614614 in __copy_helper_block_333 ()
#4  0x31ffa928 in _Block_copy_internal ()
#5  0x34a86694 in _dispatch_Block_copy ()
#6  0x34a8894e in dispatch_group_async$VARIANT$up ()
#7  0x39613c4e in -[GKConnectionInternal connectParticipantsWithConnectionData:withSessionInfo:] ()
#8  0x32c5e21c in -[GKMatch connectToPeersWithDictionaries:version:sessionToken:cdxTicket:] ()
#9  0x32c694b2 in __block_global_17 ()
#10 0x34a8711e in _dispatch_call_block_and_release ()
#11 0x34a864b6 in _dispatch_client_callout ()
#12 0x34a87dca in _dispatch_main_queue_callback_4CF$VARIANT$up ()
#13 0x3246af3a in __CFRunLoopRun ()
#14 0x323ddebc in CFRunLoopRunSpecific ()
#15 0x323ddd48 in CFRunLoopRunInMode ()
#16 0x3a1db2ea in GSEventRunModal ()
#17 0x39c992f8 in UIApplicationMain ()
#18 0x0009f370 in main ()

這些都不是我現在的代碼。

答案最終是我需要釋放

        matchRequest.minPlayers = 3;
        matchRequest.maxPlayers = 4;

重新定義它們的值之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM