我正在实现一个CLLocationManager,如几个教程中所述。
一切正常,直到LocationManager收到第二次更新。然后发生内存泄漏。
Instruments告诉我,泄漏的对象是NSCFTimer,GeneralBlock-16和NSCFSet
有什么想法吗?
感谢您的帮助
[编辑]
重复启动和停止locationManager后,更新似乎更快。这让我觉得CLLocationManager每次发生位置更新时都会初始化一个新的计时器......非常奇怪......
并且 - 所以你不需要阅读我的评论 - 应用程序在一段时间后崩溃
[编辑]
好的 - 我不知道这里的代码......
我正在为locationManager使用单独的类,如下所述: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
然后,在主要课程中我打电话:
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
这让我有点疯狂:)
答案 0 :(得分:0)
我的建议是不要沉迷于iOS本身产生的一次性内存泄漏。它在许多地方都是这样做的,而且泄漏都非常无害。
答案 1 :(得分:0)
尝试执行Build and Analyze
。我通常会发现内存泄漏和其他非语法错误。
答案 2 :(得分:0)
locationController是一个iVar,而不是一个属性,因此当你在viewDidLoad中创建它时,将它分配给_locationController不会占用所有权。
您已自动释放该对象,因此下次在事件循环中,自动释放池将耗尽并释放。
你可以通过使它成为一个retain属性(适合你的locationManager = nil),或者去除自动释放,并在dealloc中使用显式的[locationManager release]来修复它。