ios - 来自 block 的 @autoreleasepool 中的 EXC_BAD_ACCESS

标签 ios block exc-bad-access

我正在开发一个应用程序,它从加速度计读取数据(每秒 20 个样本),并使用计时器,每 5 秒获取这些数据并进行计算。 加速度计数据保存在 NSMutableArray (aceleraciones) 中,它是一个属性。然后,当计时器触发时,使用信号量将该数组复制到新数组(以便在计算完成时保存新数据)。

我在 main.m 的 @autoreleasepool 返回语句中得到 EXC_BAD_ACCESS (我没有做任何更改)。每次运行应用程序时都会出现此错误,但不是在同一时刻:它出现在其中一个计时器 block 执行中,但不是在特定时间(有时在第二次,有时在第五次,依此类推),所以我很困惑。
为了解决这个问题,我几天来一直在搜索和阅读有关内存管理的内容,但我无法做到。我猜这是关于在 block 中使用变量的错误,但我不确定。

如果有人能对这个主题有所启发,我将非常感激。

相关代码在这里:

/**
* Function to create the timer
**/
dispatch_source_t creaTimer(uint64_t interval,uint64_t leeway, dispatch_queue_t queue,dispatch_block_t block){
     dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,queue);    
 if (timer) {
     dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
     dispatch_source_set_event_handler(timer, block);
 }
 return timer;
}


/**
* IBAction which executes when an "Start" button is tapped
**/

-(IBAction) rec{

semaforoArrays = dispatch_semaphore_create(1); //creates semaphore to accessing the saved accelerometer data

__block double  *modulos;
modulos = (double *) malloc(512);       

__block DOUBLE_COMPLEX_SPLIT  A; 

/* Allocate memory for the input operands and check its availability,
 * use the vector version to get 16-byte alignment. */
A.realp = (double *) malloc(1024 * sizeof(double));
A.imagp = (double *) malloc(1024 * sizeof(double));

if (A.realp == NULL || A.imagp == NULL) {
    printf("\nmalloc failed to allocate memory for  the real FFT"
           "section of the sample.\n");
    exit(0);
}

timer = creaTimer(5ull * NSEC_PER_SEC, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0)
                  ,^{ 
   if(numCiclos>0){

     dispatch_semaphore_wait(semaforoArrays, DISPATCH_TIME_FOREVER);

        NSMutableArray *originalArray= [NSMutableArray arrayWithArray:
         self.aceleraciones]; //copy the saved data to manipulate them
        [self.aceleraciones removeAllObjects]; //remove the saved data to put into the array the new data accelerometer will have                            

     dispatch_semaphore_signal(semaforoArrays);


    int tamanno=[originalArray count];

    for(int h=0;h<1024;h++){
      A.realp[h]=0;
      A.imagp[h]=0;
    }  //i reuse the same array (to avoiding allocating it each time)                             

    for(int r=0;r<tamanno;r++){
       A.realp[r]=[[originalArray objectAtIndex:r]doubleValue];
    } //i do that to calculate the Fourier Transform but it doesn´t
       matter in the error (i get it also with this code).

    vDSP_zvabsD(&A, 1, modulos, 1, 512);        
    vDSP_vsqD(modulos,1,modulos,1,512); 

    double sum=0;

    vDSP_sveD(modulos, 1, &sum, 512);

    sum=sum/2.0;
    vDSP_vsdivD(modulos, 1, &sum, modulos, 1, 512);
    }
      numCiclos++;//variable to avoid the execution of the block the first time timer triggers (when it is started)
});    

//until here is the problematic block. I'm sure the error is before this line.



NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
if (motionManager.accelerometerAvailable) {

    motionManager.accelerometerUpdateInterval = 1.0/20.0; 
    label.text = [NSString stringWithFormat:@"Registrando"];

    [motionManager startAccelerometerUpdatesToQueue:queue withHandler:
     ^(CMAccelerometerData *accelerometerData, NSError *error){ 

         if (error) {
             [motionManager stopAccelerometerUpdates]; 
             label.text = [NSString stringWithFormat:
                           @"Error en el acelerometro: %@", error];
         }
         else{
             if(primeraLectura){
                 primeraLectura = FALSE;
                 dispatch_resume(timer); //starts timer             
             }

             dispatch_semaphore_wait(semaforoArrays, DISPATCH_TIME_FOREVER);
             [self.aceleraciones addObject:[NSNumber numberWithDouble:
        sqrt(accelerometerData.acceleration.x*accelerometerData.acceleration.x+ 
        accelerometerData.acceleration.y*accelerometerData.acceleration.y+
        accelerometerData.acceleration.z*accelerometerData.acceleration.z)]];
        //it saves the acceleration module

             dispatch_semaphore_signal(semaforoArrays);          
         }
     }];
}else{
    label.text = @"Este dispositivo no tiene acelerometro.";
}
}

最佳答案

调试这些错误的最佳方法是在仪器中。使用僵尸配置文件运行,当您执行导致访问错误的代码时,您将看到一个弹出窗口。单击右下角的箭头可查看分配/释放列表,其中一行将显示有问题的代码。

关于ios - 来自 block 的 @autoreleasepool 中的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8544925/

相关文章:

ios - 删除项目后 CollectionView 崩溃

iphone - 自定义 UIMenuController

ios - 导航栏中的图像不会按比例缩小

ios - drawRect :中的EXC_BAD_ACCESS

objective-c - 使用 deleteObject : on Core Data store 时的 EXC_BAD_ACCESS (SIGSEGV)

ios - EXC_BAD_ACCESS 当安全地展开 NSManagedObject 上的可选内容时

ios - Swift - 水平滚动 Collection View 单元格(自动和手动)

linux - 跟踪 block 设备上的脏 block

mysql - 设置了 key_block_size 的 MyISAM 表

layout - 如何以编程方式有条件地从布局中删除 block 或容器?