我有一个 nsmutablearray,我在整个应用程序中都需要它,所以我在应用程序委托中声明它并在应用程序委托的 dealloc 方法中释放它。这是代码。
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
NSMutableArray *arr1;
IBOutlet UINavigationController *navConroller;
}
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navConroller;
@property (nonatomic, retain) NSMutableArray *arr1;
@implementation AppDelegate
@synthesize navConroller;
@synthesize window = _window;
@synthesize arr1;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.arr1 = [[NSMutableArray alloc] init];
[self.window addSubview:navConroller.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[self.arr1 release];
[_window release];
[super dealloc];
}
当我检查内存性能工具时,它显示内存泄漏
self.arr1 = [[NSMutableArray alloc] init];
我在不同的类中使用这个数组。任何建议将不胜感激。
我使用了 Pieter Gunst 的答案并在那个地方停止泄漏。但它显示在另一个地方。我在哪里解析 json 并将记录存储在 arr1 中。这是代码。
-(void) apiCall:(NSString *)para1 {
SBJSON *parser = [[SBJSON alloc] init];
para1 = [para1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
para1 = [para1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [[[NSString alloc] initWithFormat:@"my api url",para1] autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
arr1 = [parser objectWithString:json_string error:nil];
[json_string release];
[parser release];
}
现在泄漏显示在下一行。
arr1 = [parser objectWithString:json_string error:nil];
有什么建议吗?