CLLocationManager - weird memory leak

I am implementing the CLLocationManager right as described in several tutorials.

Everything works fine until the LocationManager receives a second update. Then a memory leak occurs.

The tools tell me that the leaked objects are NSCFTimer, GeneralBlock-16 and NSCFSet

Any ideas?

Thanks for any help

[change]

After restarting and stopping the location manager, the updated ones seem to be faster. It makes me think that the CLLocationManager initializes a new timer every time a location update happens ... VERY weird ...

And - so you don’t need to read my comment - the application will work after a while

[change]

Ok - I don't understand the code here ...

I use a separate class for locationManager as described here: http://www.vellios.com/2010/08/16/core-location-gps-tutorial/

locationManager.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@protocol locationManagerDelegate 

@required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
@end

@interface locationManager : NSObject <CLLocationManagerDelegate>{
    CLLocationManager *myLocationManager;
    id delegate;
    CLLocation *bestEffortAtLocation;
    BOOL    outOfRange;
}

@property (nonatomic, retain) CLLocationManager *myLocationManager;  
@property (nonatomic, retain) CLLocation *bestEffortAtLocation;
@property (nonatomic, assign) id  delegate;
@property (nonatomic, assign) BOOL  outOfRange;

@end

locationManager.m

#import "locationManager.h"

@implementation locationManager

@synthesize myLocationManager;
@synthesize delegate;
@synthesize bestEffortAtLocation;
@synthesize outOfRange;

- (id) init {
    self = [super init];
    NSLog(@"initializing CLLocationManager");
    if (self != nil) {
        outOfRange = NO;

        self.myLocationManager = [[[CLLocationManager alloc] init] autorelease];
        self.myLocationManager.delegate = self;

        self.myLocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

        [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:100.0];
    }else{
        NSLog(@"Location Manager could not be initialized");
    }
    return self;
}

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

    if(outOfRange == NO){

        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];

        NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
        if (locationAge > 5.0) return;
        // test that the horizontal accuracy does not indicate an invalid measurement
        if (newLocation.horizontalAccuracy < 0) return;

        [self.delegate locationUpdate:newLocation];
    }else{
        [self.myLocationManager stopUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"error!!!!");
    [self.myLocationManager stopUpdatingLocation];
    [self.delegate locationError:error];
}

- (void)dealloc {
    [myLocationManager release];
    [bestEffortAtLocation release];
    [super dealloc];
}

@end

then in the main class I call:

mainFile.h (exerpt)

#import "locationManager.h"

@interface mainFile : UIViewController  <locationManagerDelegate , UIAlertViewDelegate>{
    locationManager *locationController;
    CLLocation      *myLocation;
}

@end

mainFile.m (exerpt)

#import "locationManager.h"

@implementation mainFile

@synthesize locationController;
@synthesize myLocation;

- (void)locationError:(NSError *)error{
// Do alert-Stuff
}

- (void)locationUpdate:(CLLocation *)location {
// Do location-Stuff
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationController = [[[locationManager alloc] init] autorelease];

    locationController.delegate = self;
    [locationController.myLocationManager startUpdatingLocation];
}

- (void)dealloc {
    self.locationController = nil;
    [locationController release];
}

@end

It drives me crazy:)

+3
source share
3 answers

My advice is not to be obsessed with the one-time memory leaks that iOS itself generates. He does this in many places, and the leaks are all pretty harmless.

0
source

Try to complete Build and Analyze. I usually detect memory leaks and other non-syntax errors.

0
source

, , .

locationController - iVar, , , viewDidLoad, _locationController .

, .

You can fix this by making this save property (which will match your locationManager = nil) or get rid of automatic release and using the explicit [locationManager release] in dealloc.

0
source

Source: https://habr.com/ru/post/1760845/


All Articles