1

In my application i declare an array property,

@property (nonatomic, retain) NSArray *listOfItems;

and in my viewDidLoad method,

listOfItems = [[NSArray alloc] initWithObjects:@"First", @"Second", nil];

I do not release the array in my viewDidLoad, because the objects in the array will be required elsewhere in the application.
Finally in my dealloc method i put,

[listOfItems release];

My question is: Is there a memory leak in this code? The retain count should be increased twice due to the (retain) in the property as well as the alloc in the viewDidLoad, but only decreased once in the dealloc method.

2
  • No memory leak mate it's all good, but just incase you want to double chech clean the built and do an ANALYSE it will show you the leacks Commented May 31, 2011 at 13:19
  • I think there are no leaks ... You alloc 1 time and then release ... Commented May 31, 2011 at 13:19

6 Answers 6

1

the retain will only 'kick in' when you do it like this

self.listOfItems = [[NSArray alloc] initWithObjects:...];

Now, the retain count is indeed 2. If you leave self out, it will just be one. There is a distinct difference in calling 'set' and just assigning.

To answer your original question; your code is not leaking.

Sign up to request clarification or add additional context in comments.

Comments

0

Since your setter retains the array, you must release it, after you allocated and set it in your viewDidLoad method (with this notation I suggest "autorelease").

But perhaps it's easier for you, if you use [NSArray arrayWitObjects:].

Comments

0
NSArray *array=[[NSArray alloc] initWithObjects:@"First",@"Second",nil];
self.listOfItems=array;
[array release];

You, can use this way also.

Comments

0

Just like @MiRAGe said,

listOfItems = [[NSArray alloc] initWithObjects:@"First", @"Second", nil];

The retain count will be 1

but if the code is

self.listOfItems = [[NSArray alloc] initWithObjects:@"First", @"Second", nil];

The retain count will be 2

Comments

0

Yes, there's a potential leak. Consider:

  1. In -viewDidLoad, you set up listOfItems as you've described.
  2. Subsequently, some other code calls yourObject.listOfItems = someNewList;.
  3. Your -dealloc releases listOfItems.

I realize that #2 above probably doesn't happen in your code, but a month from now you may decide that you need to update the list for some reason and introduce this problem. Think about what happens if #2 ever does occur...

  • Your listOfItems ivar suddenly points to a different list.
  • You no longer have a reference to the old list, so it can never be released. That's your leak.

It's much better to release the array in -viewDidLoad after you've assigned it to your property.

Comments

0

It wont be confusing if you practice like this

@property (nonatomic, retain) NSArray *listOfItems;  
NSArray *temparray = [[NSArray alloc] initWithObjects:@"First", @"Second", nil];  
self.listOfItems = temparray;  
[temparray release];

and in dealloc

[listOfItems release];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.