nsmutablearray fuga de memoria iphone

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];

Cualquier sugerencia ?

preguntado el 31 de julio de 12 a las 10:07

it does not seem to be leaked. -

3 Respuestas

You have allocated array and also retained it by accessing property. Change below line of code;

self.arr1 = [[NSMutableArray alloc] init];

with either of any below line..

arr1 = [[NSMutableArray alloc] init];


self.arr1 = [NSMutableArray array];


NSMutableArray *tArr1 = [[NSMutableArray alloc] init];
self.arr1 = tArr1;
[tArr1 release];

Respondido 31 Jul 12, 10:07

@BoA you're wrong. He's using non-arc. In ARC code you never call [super dealloc]; - tik hop

@tikhop I've been coding too much Android recently, perhaps he isn't. But anyway, he was releasing it in dealloc so there shouldn't be an autorelease there anyway. - bo a

@BoA Yeah, however if you see "strong" attribute in the code this doesn't mean that the code use ARC. strong equivalent to retain in non-ARC code - tik hop

@tikhop Yeah, I realised that. - bo a

i tried both this post self.arr1 = [NSMutableArray array]; and @synthesize arr1 = _arr1; and _arr1 = [[NSMutableArray alloc] init]; both are working well. I am not getting the difference. can any one explain ? - Narshana viral

Trata @synthesize arr1 = _arr1; y _arr1 = [[NSMutableArray alloc] init];

Respondido 31 Jul 12, 10:07

Thanks for answering. I tried both this post self.arr1 = [NSMutableArray array]; and @synthesize arr1 = _arr1; and _arr1 = [[NSMutableArray alloc] init]; both are working well. I am not getting the difference. can you please explain ? - Narshana viral

As Apurv already mentioned use

self.arr1 = [NSMutableArray array];


self.arr1 = [[[NSMutableArray alloc] init] autorelease];

Also make all properties of same type (don't mix strong and retain):

@property (strong, nonatomic)

And for dealloc method use automatically generated getter/setter:

- (void)dealloc {
    self.arr1 = nil;
    self.window = nil;
    [super dealloc];


Topic starter does not use ARC, because of

[super dealloc];

cause error "ARC forbids explicit message send of 'dealloc'"

Respondido 31 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.