I have an nsmutablearray which i need through out the application so i declared it in application delegate and release it in dealloc method of application delegate. Here is the code for that.
@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];
}
when i check memory performance tool it shows me memory leak at
self.arr1 = [[NSMutableArray alloc] init];
I am using this array in different classes. Any suggestion will be appreciated.
I used Pieter Gunst answer and leak stop at that place. but it is showing at another place. where i pares json and storing records in arr1. Here is the code for that.
-(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];
}
Now leak shows at following line.
arr1 = [parser objectWithString:json_string error:nil];
Any suggestion ?
You have allocated array and also retained it by accessing property.
Change below line of code;
with either of any below line..
OR
OR