我正在实现一个CLLocationManager权限,如几个教程中所述。CLLocationManager - 奇怪的内存泄漏
一切正常,直到LocationManager收到第二个更新的点。然后发生内存泄漏。
仪器告诉我,泄漏对象是NSCFTimer,GeneralBlock-16和NSCFSet
任何想法?
感谢所有帮助
[编辑]
repeatingly启动和停止的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
然后,在主I类拨打:
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
这让我有点疯狂:)
应用程序崩溃后,说每次100秒?因为它应该发送一个延迟的消息给一个没有实现该消息的方法的对象。它也应该随机崩溃,因为你释放位置管理器,但你仍然保持对它的引用。 – 2010-08-21 02:55:02
@Swissdude得到了这个问题的解决方案? – 2013-04-03 10:05:13
所以,它是一个真正的内存泄漏?的位置框架?还是在你的代码? – 2013-08-15 11:39:21