iPhone Motion - EXC 访问错误

发布于 2024-10-04 15:54:51 字数 1120 浏览 8 评论 0原文

我开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2024-10-11 15:54:53

EXC_BAD_ACCESS 是操作系统级别的异常,意味着您正在尝试访问不属于您的内存。我认为这与你的块在范围内是本地的有关,所以一旦它超出范围,它就会被销毁。您需要在堆上创建它的副本。

尝试这个答案 来自著名的戴夫·德隆。另外,与正常的 Cocoa 内存管理规则一样,如果您创建了副本,请不要忘记release它。

例如:

motionHandler = Block_copy(^ (CMDeviceMotion *motionData, NSError *error) {
    if (motionData.rotationRate.z > 5.5 || motionData.rotationRate.z < -5.5) {
        NSLog(@"Rotation of Z.");  // Reference A       
    }
});


// and then later:

- (void) dealloc
{
    [motionHandler release];
    //and all others.
    [super dealloc];
}

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:

motionHandler = Block_copy(^ (CMDeviceMotion *motionData, NSError *error) {
    if (motionData.rotationRate.z > 5.5 || motionData.rotationRate.z < -5.5) {
        NSLog(@"Rotation of Z.");  // Reference A       
    }
});


// and then later:

- (void) dealloc
{
    [motionHandler release];
    //and all others.
    [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文