gpt4 book ai didi

iphone - GKSession 分配/释放/分配 = 泄漏和崩溃

转载 作者:行者123 更新时间:2023-12-03 20:57:53 29 4
gpt4 key购买 nike

我有一个应用程序,它会抛出 GKSession 并在各种条件下(连接超时、 session 失败等)创建一个新的 GKSession。不过,我遇到了内存泄漏问题,并且有时会在重新连接几次循环后崩溃。

这是我的代码:

- (void) netConnect:(id) sender {
NSLog(@"allocating GKSession");

currentSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
currentSession.delegate = self;
currentSession.available = YES;
currentSession.disconnectTimeout = 0;
[currentSession setDataReceiveHandler: self withContext:nil];
}

- (void) netDisconnect:(id) sender {
NSLog(@"DISCONNECTING BY REQUEST");

[currentSession disconnectFromAllPeers];
[currentSession setAvailable:NO];
[currentSession setDelegate:nil];
[currentSession setDataReceiveHandler:nil withContext:nil];
[currentSession release];
currentSession = nil;
}

有一个计时器等待连接成功;如果没有收到,则调用 netDisconnect,然后在 5 秒 NSTimer 延迟后再次调用 netConnect。

我遇到了泄漏的 GKList、GKTable 和 GKAutoPeerIDTable 对象,并且像这样崩溃(总是在分配之后发生):

  Date/Time:       2010-09-05 09:35:59.426 -0700
OS Version: iPhone OS 4.0.2 (8A400)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000a
Crashed Thread: 0

Thread 0 Crashed:
0 libobjc.A.dylib 0x0000286c objc_msgSend + 16
1 CoreFoundation 0x000756ea __CFCopyFormattingDescription + 174
2 CoreFoundation 0x00081c8e __CFStringAppendFormatCore + 8666
3 CoreFoundation 0x000146ac _CFStringCreateWithFormatAndArgumentsAux + 64
4 CoreFoundation 0x00014660 CFStringCreateWithFormatAndArguments + 16
5 CoreFoundation 0x0001463c CFStringCreateWithFormat + 16
6 SystemConfiguration 0x00003272 SCDynamicStoreCreateWithOptions + 62
7 SystemConfiguration 0x000033d0 SCDynamicStoreCreate + 12
8 GameKitServices 0x000020ce gckRegisterForNetworkChanges + 154
9 GameKitServices 0x000035c0 GCKSessionCreate + 788
10 GameKitServices 0x00035e7a -[GKSessionInternal initWithSessionID:displayName:session:sessionMode:] + 274
11 GameKit 0x0000fda8 -[GKSession initWithSessionID:displayName:sessionMode:] + 76

这是另一个:

Sun Sep  5 10:28:52 thinner someapp[424] <Warning>: allocating GKSession
Sun Sep 5 10:28:52 thinner someapp[424] <Error>: -[__NSCFData UTF8String]: unrecognized selector sent to instance 0x10a710
Sun Sep 5 10:28:52 thinner someapp[424] <Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData UTF8String]: unrecognized selector sent to instance 0x10a710'
*** Call stack at first throw:
(
0 CoreFoundation 0x31a9ffd3 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x3428a8a5 objc_exception_throw + 24
2 CoreFoundation 0x31aa3a77 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
3 CoreFoundation 0x31aa2f15 ___forwarding___ + 508
4 CoreFoundation 0x31a35680 _CF_forwarding_prep_0 + 48
5 GameKitServices 0x327aff13 -[GKSessionInternal initWithSessionID:displayName:session:sessionMode:] + 426
6 GameKit 0x31c15daf -[GKSession initWithSessionID:displayName:sessionMode:] + 82
7 someapp 0x0000584b -[MainViewController netConnect:] + 126

问题:
• 这是释放和重新分配 GKSession 的正确方法吗?
• 我不应该首先在连接之间拆除并重新分配 GKSession 吗?

最佳答案

这是一个已知问题。解决方法是不要为 sessionID 传入 nil:[[GKSession alloc] initWithSessionID:@"com.put.something.here"displayName:nil sessionMode:GKSessionModePeer];

关于iphone - GKSession 分配/释放/分配 = 泄漏和崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3646974/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com