iPhone Motion - EXC 访问错误
我开始使用 DeviceMotion 类进行编码。在遵循Apple的文档之后,我得到了以下内容:
- (void)viewDidLoad {
[super viewDidLoad];
myMM = [[CMMotionManager alloc] init];
myMM.deviceMotionUpdateInterval = 1.0/30.0;
theQ = [[NSOperationQueue currentQueue] retain];
motionHandler = ^ (CMDeviceMotion *motionData, NSError *error) {
if (motionData.rotationRate.z > 5.5 || motionData.rotationRate.z < -5.5) {
NSLog(@"Rotation of Z."); // Reference A
}
};
-(IBAction)toggleClick{
NSLog(@"toggle");
if(myMM.gyroAvailable){
if(myMM.deviceMotionActive){
NSLog(@"Stopping Motion Updates..");
[myMM stopDeviceMotionUpdates];
} else {
NSLog(@"Starting Motion Updates..");
[myMM startDeviceMotionUpdatesToQueue:theQ withHandler:motionHandler];
}
}
else {
NSLog(@"No motion available. Quit!");
}
此代码工作正常,但是当我想要执行除 NSLog 之外的任何代码(甚至是像递增整数这样简单的事情)来代替“引用 A”时',我在控制台中收到 EXEC Bad Access。
我环顾四周,发现这是某种内存泄漏。有谁知道发生了什么事吗?如果不是,我怎样才能弄清楚?我对仪器缺乏经验,但如果我指出正确的方向,我将不胜感激。
I'm beginning coding with the DeviceMotion class. After following Apple's documenation, i have the following:
- (void)viewDidLoad {
[super viewDidLoad];
myMM = [[CMMotionManager alloc] init];
myMM.deviceMotionUpdateInterval = 1.0/30.0;
theQ = [[NSOperationQueue currentQueue] retain];
motionHandler = ^ (CMDeviceMotion *motionData, NSError *error) {
if (motionData.rotationRate.z > 5.5 || motionData.rotationRate.z < -5.5) {
NSLog(@"Rotation of Z."); // Reference A
}
};
-(IBAction)toggleClick{
NSLog(@"toggle");
if(myMM.gyroAvailable){
if(myMM.deviceMotionActive){
NSLog(@"Stopping Motion Updates..");
[myMM stopDeviceMotionUpdates];
} else {
NSLog(@"Starting Motion Updates..");
[myMM startDeviceMotionUpdatesToQueue:theQ withHandler:motionHandler];
}
}
else {
NSLog(@"No motion available. Quit!");
}
This code works fine, however when I want to do any code except an NSLog (even something as simple as incrementing an integer) in place of the 'reference A', I get an EXEC Bad Access in the console.
I've looked around, and all I've found is that it's a memory leak of sorts. Does anyone know whats going on? If not, how can I figure it out? I'm pretty inexperienced with Instruments, but if I'm pointed in the right direction I'd be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EXC_BAD_ACCESS
是操作系统级别的异常,意味着您正在尝试访问不属于您的内存。我认为这与你的块在范围内是本地的有关,所以一旦它超出范围,它就会被销毁。您需要在堆上创建它的副本。尝试这个答案 来自著名的戴夫·德隆。另外,与正常的 Cocoa 内存管理规则一样,如果您创建了副本,请不要忘记
release
它。例如:
EXC_BAD_ACCESS
is an OS-level exception meaning that you are trying to access memory that doesn't belong to you. I think this has something to do with your block being local to the scope, so once it goes out of scope, it is destroyed. You need to create a copy of it on the heap.Try this answer from the renowned Dave DeLong. Also, as with the normal Cocoa memory management rules, don't forget to
release
it if you've made a copy.For example: