使用 GameKit 通过蓝牙发送数据崩溃
我正在尝试使用下面的方法通过 GameKit 蓝牙发送简单的数据。然而,当我的 2 台设备成功连接并点击按钮发送时,它只是在显示“EXC_BAD_ACCESS”的最后一行崩溃。知道出了什么问题吗?
- (IBAction)sendAction:(id)sender {
// convert an NSString to NSData
NSString *str = @"Yahooooo!!";
NSData *data = [str dataUsingEncoding: NSASCIIStringEncoding];
[currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
}
I'm trying to use the below method to send simple data through GameKit Bluetooth. However, when my 2 devices successfully connect and I tap the button to send, it just crashes at the last line showing "EXC_BAD_ACCESS". Any idea what's wrong?
- (IBAction)sendAction:(id)sender {
// convert an NSString to NSData
NSString *str = @"Yahooooo!!";
NSData *data = [str dataUsingEncoding: NSASCIIStringEncoding];
[currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有正确管理 currentSession 变量的保留计数。假设 currentSession 是一个成员变量,当您分配给 currentSession 时,如果它不是由名为 alloc 或 copy 的方法返回,您需要保留它直到您使用完它,此时您应该释放它。
您应该查看内存管理编程指南:
http: //developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
You are probably not managing the retain count of the currentSession variable properly. Assuming that currentSession is a member variable, when you assign to currentSession, if it's not being returned by a method called alloc or copy, you need to retain it until you are done with it, at which point you should release it.
You should check out the Memory management Programming Guide:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html