我一直在努力解决程序中的内存泄漏问题,而我却陷入了一些困境。奇怪的是,当我使用CoreLocation获取gps位置时,它们就来了。代码正确返回该位置,但它在整个地方泄漏:CFHTTPMessage,CFURLConnection,CFURLRequest,CFURLResponse,GeneralBlock-16,-32,-48,HTTPRequest等...有谁可以指导我如何解决这个问题?
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
答案 0 :(得分:3)
您需要释放LocationManager。确保在释放之前将其委托设置为NULL。而且,返回第一个结果并不是一个好主意。确保水平精度不低于0也低于某个阈值,如5000米。为了使其更加健壮,您可能需要添加一个计时器,以便在给定的时间后停止,如果它无法准确找到您的位置。
答案 1 :(得分:1)
你永远不会发布locationController