1

我一直在努力解决我的程序中的内存泄漏问题,但我遇到了一些落后者。奇怪的是,当我使用 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
4

2 回答 2

3

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

于 2010-07-16T21:28:24.453 回答
1

你永远不会释放locationController

于 2009-08-24T04:45:43.453 回答