2009-08-24 37 views
1

我一直在努力解决程序中的内存泄漏问题,并且我只是为了解决一些碎片问题。奇怪的是,他们来自我使用CoreLocation获取gps位置。代码正确地返回位置,但它泄漏到各地:CFHTTPMessage,CFURLConnection,CFURLRequest,CFURLResponse,GeneralBlock-16,-32,-48,HTTPRequest等...任何人都可以请指导我如何解决这个问题?iPhone Help:CoreLocation框架中的奇怪内存泄漏

的MyCLController

locationController = [[MyCLController alloc] init]; 
locationController.delegate = self; 
[locationController.locationManager startUpdatingLocation]; 

初始化做一些事情,并得到一个回调通过委托:

[locationController release]; 

MyCLController.h:

#import <Foundation/Foundation.h> 
@protocol MyCLControllerDelegate 
@required 
- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSError *)error; 
@end 
@interface MyCLController : NSObject { 
    CLLocationManager *locationManager; 
    id delegate; 
} 
@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (nonatomic, assign) id delegate; 

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

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error; 
@end 

MyCLController.m:

#import "MyCLController.h" 
@implementation MyCLController 
@synthesize locationManager; 
@synthesize delegate; 

- (id) init { 
    self = [super init]; 
    if (self != nil) { 
     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; // send loc updates to myself 
    } 
    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 
    [locationManager stopUpdatingLocation]; 

    [self.delegate locationUpdate:newLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error { 
    [self.delegate locationError:error]; 
} 
- (void)dealloc { 
    [super dealloc]; 
} 
@end 

回答

3

你需要释放LocationManager。确保在发布之前将其委托设置为NULL。而且,返回第一个结果并不是一个好主意。确保水平精度不是< 0,也低于某个阈值,如5000米。为了使它更加强大,如果无法准确找到您的位置,您可能需要添加一个计时器,以便在给定时间后停止计时。

+0

这很有用。 – JonLeah 2010-07-20 14:40:58

1

你永远不会释放locationController

+0

感谢您的回答,但这实际上不是泄漏来自何处,我忘了发布我的dealloc方法。当我调用startUpdatingLocation时,发生泄漏。 [locationController release]; – JonLeah 2009-08-24 04:50:42