2010-08-21 64 views
3

我正在实现一个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 

这让我有点疯狂:)

+0

应用程序崩溃后,说每次100秒?因为它应该发送一个延迟的消息给一个没有实现该消息的方法的对象。它也应该随机崩溃,因为你释放位置管理器,但你仍然保持对它的引用。 – 2010-08-21 02:55:02

+0

@Swissdude得到了这个问题的解决方案? – 2013-04-03 10:05:13

+0

所以,它是一个真正的内存泄漏?的位置框架?还是在你的代码? – 2013-08-15 11:39:21

回答

0

我的建议是不要痴迷iOS本身产生的一次性内存泄漏。它在很多地方都是这样做的,泄漏都非常无害。

+0

但应用程序崩溃后一段时间 - 所以我想,我应该担心;) – Swissdude 2010-08-21 01:09:20

+0

然后,你有更大的问题。发布更多的代码,如果你想提示。 – 2010-08-21 01:33:01

+0

一个小型的内部iOS泄漏不太可能导致您的应用崩溃。如果您需要帮助,请将崩溃日志发布到另一个问题中。 – raidfive 2010-08-21 02:44:47

0

试试做Build and Analyze。我通常通过这种方式发现内存泄漏和其他非语法错误。

0

啊,很死的问题,我爱他们。

locationController是伊娃,而不是一个性质,所以当你在viewDidLoad中创建它,将其分配给_locationController不采取所有权。

你自动释放对象,围绕事件循环,使接下来的时间,自动释放池排水,它被释放。

您可以通过使其成为一个保留属性(这将适合您的locationManager = nil),或摆脱自动释放,并在dealloc中使用明确的[locationManager发布]来修复它。