0

I am using MKReverseGeocoder (reversegeocoder) to retrieve user's current location. I am calling reversegeocoder in locationDidUpdatedToLocation method of CLLocation Manager.

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

if(!reverseGeocoder )
    {
        NSLog(@"geocoder is nill");
        reverseGeocoder=[[MKReverseGeocoder alloc] initWithCoordinate:[newLocation coordinate]];
        reverseGeocoder.delegate=self;
        [reverseGeocoder start];
        NSLog(@"geocoder allocated");
    }

    else
    {
        NSLog(@"geocoder is not nill");


        if(reverseGeocoder.querying)
        {
        //do nothing ;
        }
        else
        [reverseGeocoder release];

    }

}

.querying property is used to ensure that reversegeocoder should be released only when It has completed its query. But when application crashes within 3-4 seconds of running with error message

-[MKReverseGeocoder isQuerying]: message sent to deallocated instance

What am I missing here?

Thanks for any help in advance.

alekhine
  • 1,600
  • 4
  • 20
  • 45

1 Answers1

1

Could it be because you haven't set the geocoder's delegate to nil before releasing it? Or that you haven't nilled the pointer?

Try this:

    if(reverseGeocoder.querying)
    {
            //do nothing ;
    }
    else
    {
            [reverseGeocoder setDelegate:nil];
            [reverseGeocoder release];
            reverseGeocoder = nil;
    }
nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Glad to hear. As a piece of advice, I'd move that code out of the `didUpdateToLocation` and instead use a timer to control when geocoding is performed. If you geocode too often or too quickly, you might start getting errors. – nevan king Sep 14 '11 at 14:31
  • Thanks for the advice. I will implement your suggestion and see if it's better option. – alekhine Sep 14 '11 at 14:47