0

I have searched StackOverflow for similar topics; there are two similar posts, but I am a bit of a dummy and do not understand the answers given. My issue is that when I run this code without specifying that it should go on a dispatch queue, the live bytes settle down and do not keep increasing, but when I specify that it should go on a dispatch queue the live bytes eventually go through the roof. The stuff that goes on between

//Do lots of stuff here

//Finish lots of stuff here

is pretty heavy so I would like it not on the main thread: I want the results to provide a continuous update to the rest of the algorithm. Also, I get occasional bizarre behavior in the graphical display when the dispatch lines are commented out. But using the dispatch queue the way I have written it causes the live bytes to blow up, while if I comment these lines out the live byte number settles down to a constant level.

What am I doing wrong? Thanks in advance!

I start the NSTimer in the main thread:

 -(void) startGeoHardware
 {
     if (self.geoHardwareArrayTimer == nil)
     {

         self.geoHardwareArrayTimer = [NSTimer      scheduledTimerWithTimeInterval:arrayTimerUpdate
                                                                  target:self
                                                                selector:@selector(startGeoHardWareArray:)
                                                                userInfo:nil
                                                                 repeats:YES];

    }

 }

and then call the method

 - (void) startGeoHardWareArray: (NSTimer *)geoHardwareArrayTimer  //: (CMCalibratedMagneticField)field
 {


     dispatch_queue_t runGeoHardwareArrayQueue = dispatch_queue_create("GeoHardwareArray",NULL);
     dispatch_async(runGeoHardwareArrayQueue, ^{

      //Do lots of stuff here

      //Finish lots of stuff here

     });
     dispatch_release(runGeoHardwareArrayQueue);

 }

The geoHardwareArrayTimer is defined in the .h file as

 NSTimer *geoHardwareArrayTimer;
 }
 @property(nonatomic,retain) NSTimer *geoHardwareArrayTimer;

and is synthesized in the .m file. and is deallocated

     if (self.geoHardwareArrayTimer != nil)
     {
         [self.geoHardwareArrayTimer invalidate];
         self.geoHardwareArrayTimer = nil;
     }

     [super dealloc];
Antonio Favata
  • 1,891
  • 1
  • 15
  • 13
user1976727
  • 71
  • 10

1 Answers1

0

OK, this is not exactly a very good answer -- it 1) sort of works, and 2) raises another, similar question -- NOW where is the leak coming from???

In searching more posts I discovered the dispatch timer method. This seems to work very well for what I want: In this simple example I am creating and running two timers and running the methods on separate queues -- and the live bytes soon appear to settle down, more or less, to a near constant. BUT, there is a one-time leak at the beginning:

OS_dispatch_source libdispatch.dylib _os_object_alloc_realized

which means absolutely nothing to me.

Any ideas??? Thanks

Tim

- (void)viewDidLoad
{
    [self createDispatchTimers];
}

- (void)createDispatchTimers
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    dispatch_source_t geoHardwareArrayTimer = CreateDispatchTimer(arrayTimerUpdate * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / arrayTimerLeeway, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Repeating task
        [self startGeoHardWareArray:geoHardwareArrayTimer];


    });


    dispatch_source_t geoHardwareTimer = CreateDispatchTimer(timerUpdate * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / timerLeeway, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Repeating task
        [self runGeoHardware:geoHardwareTimer];

    });


}





dispatch_source_t CreateDispatchTimer(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);
        dispatch_resume(timer);
    }
    return timer;

}

void RemoveDispatchSource(dispatch_source_t timer)
{
    dispatch_source_cancel(timer);
    dispatch_release(timer);
}








- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// Here are the dispatch queue Methods ///////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

- (void) startGeoHardWareArray: (dispatch_source_t) geoHardwareArrayTimer
{
    dispatch_queue_t runGeoHardwareArrayQueue = dispatch_queue_create("GeoHardwareArray",0);
    dispatch_async(runGeoHardwareArrayQueue, ^{

        NSLog(@"Array Timer running");

    });
    dispatch_release(runGeoHardwareArrayQueue);
}


- (void) runGeoHardware: (dispatch_source_t) geoHardwareTimer
{
    dispatch_queue_t runGeoHardwareQueue = dispatch_queue_create("GeoHardware",0);
    dispatch_async(runGeoHardwareQueue, ^{

        NSLog(@"Hardware Timer running");

    });
    dispatch_release(runGeoHardwareQueue);
}
user1976727
  • 71
  • 10