Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:34:57+00:00 2026-05-27T13:34:57+00:00

I am developing an application which reads from the accelerometer (20 samples per second)

  • 0

I am developing an application which reads from the accelerometer (20 samples per second) and, using a timer, each 5 seconds takes these data and do calculations with them.
The accelerometer data are saved in an NSMutableArray (aceleraciones) which is a property. Then, when the timer triggers, this array is copied to a new one using a semaphore (in order to saving new data while the calculations are done).

I get an EXC_BAD_ACCESS in @autoreleasepool return sentence in main.m (i’ve done no changes there). I have this error everytime I run the app but not in the same moment: it appears in one of the timer block executions but not in a specific time (sometimes in the second time, sometimes in the fifth, and so on)so I am very puzzled.
I have been searching and reading about memory management for days in order to solve it but i couln’t do it. I guess it is an error about the using of variables into blocks, but I’m not sure.

I would be very grateful if somebody could throw any light on the subject.

The relevant code is here:

/**
* 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.";
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:34:58+00:00Added an answer on May 27, 2026 at 1:34 pm

    Look in motionManager startAccelerometerUpdatesToQueue:. Very likely you save the queue parameter in a property there, and then release it in your dealloc. But when you placed the queue value into the property you didn’t retain it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing, in VB.Net, an application that reads from text files using a
I am developing a Application which takes two Files and output will be two
I'm developing an application which draws some charts and I'm using Google Chart. After
Okay, I am developing an Application which requires getting sensitive information from a database
I'm developing an application which read some data from a db. The connection to
I'm developing a small application that reads data from a database and displys it
i am developing an web application in which i have to read binary from
I am developing an application which requires the user to drag a file from
I am developing a GWT application which im using Spring Security to handle the
I'm developing an application under C# to read some data from a hardware which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.