Search code examples
iphonememory-managementmkreversegeocoder

Problem with running MKReverseGeocoder in background, iphone


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.


Solution

  • 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;
        }