0

在为另一个对等方(不是自己)调用 displayNameForPeer 后释放 GKSession 时,我可以可靠地使模拟器崩溃,而且我不确定这是我做错了什么还是苹果的 Gamekit 框架的错误(以及我是否需要担心,因为我只看到 4.0 和 4.1 下的崩溃,而不是 4.2+)。

输出是:

found available peer; checking name and ID... m4, 26176566
*** -[GKSessionInternal lock]: message sent to deallocated instance 0x7508900

这是最小的可重现代码集——请注意,另一个 GKSession 必须在网络上可见(以便找到一个可用的对等方来调用 displayNameForPeer)才能触发崩溃。在另一台设备上运行相同的代码但没有 makeUnavailable 和 killSession 调用就足够了。

- (void)viewDidLoad
{

[self createSession];
[self makeAvailable];

peerListAvailable = [[NSMutableArray alloc] initWithArray:[currentSession peersWithConnectionState:GKPeerStateAvailable]];
for (NSString *peer in peerListAvailable)
{
    // this method guarantees the crash on session release
    NSLog(@"found available peer; checking name and ID... %@, %@",[currentSession displayNameForPeer:peer], peer);
}
[peerListAvailable release];
peerListAvailable = nil;


[self makeUnavailable];
[self killSession];

[super viewDidLoad];
}


- (void) createSession
{
if (!currentSession)
{
    currentSession = [[GKSession alloc] initWithSessionID:@"GKTester" displayName:nil sessionMode:GKSessionModePeer];
    currentSession.delegate = self;
    currentSession.disconnectTimeout = 30;
    [currentSession setDataReceiveHandler: self withContext:nil];
}

}

-(void) killSession
{
if (currentSession) 
{
    [currentSession disconnectFromAllPeers];
    [currentSession setDelegate:nil];
    [currentSession setDataReceiveHandler:nil withContext:nil];
    [currentSession release]; // crash occurs after this
    currentSession = nil;   
}
}

-(void) makeAvailable
{
while (currentSession && !currentSession.available)
{
    [currentSession setAvailable:YES];
    [NSThread sleepForTimeInterval:.5];
}
}

-(void) makeUnavailable
{
while (currentSession && currentSession.available)
{
    [NSThread sleepForTimeInterval:.5];
    [currentSession setAvailable:NO];
}

}

4

1 回答 1

0

您的代码中有过度发布:

[currentSession disconnectFromAllPeers];
[currentSession setDelegate:nil];
[currentSession setDataReceiveHandler:nil withContext:nil];
[currentSession release]; // This is an over-release
currentSession = nil; // You are trying to access a variable after it's been released

您应该只在 dealloc 中释放 currentSession 成员变量,如下所示:

- (void)dealloc
{
    [currentSession release];
    [super dealloc];
}
于 2012-01-16T01:05:56.043 回答