【问题标题】:CLLocationManager - strange Memory LeakCLLocationManager - 奇怪的内存泄漏
【发布时间】:2011-04-01 22:29:48
【问题描述】:

我正在实现一个 CLLocationManager 权限,如几个教程中所述。

在 LocationManager 收到第二次更新之前一切正常。然后发生内存泄漏。

仪器告诉我,泄露的对象是 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(摘录)

#import "locationManager.h"

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

@end

mainFile.m(摘录)

#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 秒?因为它应该是因为您正在向未实现该消息的方法的对象发送延迟消息。它也应该随机崩溃,因为你释放了位置管理器,但你仍然保持对它的引用。
  • @Swissdude 有解决这个问题的办法吗?
  • 那么,这是真正的内存泄漏吗?位置框架?还是在您的代码中?

标签: iphone memory-leaks cllocationmanager


【解决方案1】:

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

【讨论】:

  • 但是应用程序会在一段时间后崩溃 - 所以我想,我应该担心 ;)
  • 那你有更大的问题。如果您需要提示,请发布更多代码。
  • 小的内部 iOS 泄漏不太可能导致您的应用程序崩溃。如果您需要帮助,请在另一个问题中发布崩溃日志。
【解决方案2】:

尝试使用Build and Analyze。我通常会以这种方式发现内存泄漏和其他非语法错误。

【讨论】:

    【解决方案3】:

    啊,一个长期死的问题,我爱他们。

    locationController 是一个 iVar,而不是属性,因此当您在 viewDidLoad 中创建它时,将其分配给 _locationController 不会取得所有权。

    你已经自动释放了对象,所以下次在事件循环中,自动释放池被耗尽并被释放。

    您可以通过将其设置为保留属性(这将适合您的 locationManager = nil )或摆脱自动释放并在 dealloc 中使用显式 [locationManager release] 来修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-06
      • 2015-05-09
      • 2021-03-28
      • 2020-12-19
      • 2012-03-07
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多