0

This is a basic question, and I'm not really sure what to search for to see if its been asked before.

In a lot of examples, I've seen property assignments handled like this:

NSArray *tempArray = [/*some code to get array*/];
self.propertyArray = tempArray;
[tempArray release];

Why not just do:

self.propertyArray = [/*some code to get array*/];

What's the difference between the two?

Snowman
  • 31,411
  • 46
  • 180
  • 303
  • Go read this post, it might help you -> http://stackoverflow.com/questions/5456848/one-alloc-retaincount-2/5456895#5456895 [1]: http://stackoverflow.com/questions/5456848/one-alloc-retaincount-2/5456895#5456895 – klefevre Apr 22 '12 at 15:05
  • possible duplicate of [Objective-C Properties and Memory Management](http://stackoverflow.com/questions/2292880/objective-c-properties-and-memory-management) – Paul.s Apr 22 '12 at 15:05

5 Answers5

1

In the second example, you don't release the object, which is retained by the property, so you have a memory leak.

self.propertyArray = [[SomeClass alloc] init];
//                               ^ no release
MByD
  • 135,866
  • 28
  • 264
  • 277
  • 3
    Obviously not if you `[[[SomeClass alloc] init] autorelease]`. Or just use ARC, which takes care of releasing and autoreleasing for you, in which case, again, you don't have any leak either. (Personally, I don't get why anyone would have a new project and not use ARC.) – Rob Apr 22 '12 at 15:28
  • You wouldn't necessarily have a memory leak, your assuming he's not using the Class method self.propertyArray = [NSArray arrayWith...] – Vikings Apr 22 '12 at 17:50
  • For both commenters - first - you are right, but I gave a concrete example. the basic idea is that you have more control over objects with reference to them, then anonymous objects. I would use autorelease in most cases, and then there's no real difference, but under some circumstances one option has an advantage. – MByD Apr 22 '12 at 17:56
1

With ARC, the two statements are equivalent in practice (although you would have to omit the release call for it to actually compile in ARC).

In a manual managed memory scenario, you would leak tempArray in the second ("direct assignment", which it isn't because you're calling a property setter not setting an ivar) example, as you do not have a release call on tempArray to balance it's alloc/init.

The the useful distinction is reduced to expressiveness, the ability to debug, and ultimately the programmers personal preference.

isaac
  • 4,867
  • 1
  • 21
  • 31
1

This answer is assuming your not using ARC.

The first code snippet, is the way Apple recommends initializing a property as long as you are doing this in any method besides init. You'll notice Apple code snippets do this a lot. It makes it much easier to manage your memory, and in my opinion it makes it easier to read.

NSArray *tempArray = [[NSArray alloc] initWith....]
self.propertyArray = tempArray;
[tempArray release];

The second code snippet you have could potential lead to a memory leak depending how you set up the NSArray.

This would be a leak. The propertyArray would have an retain count of 2. After you release the instance variable in dealloc, you still have a retain count of 1. That is a leak.

self.propertyArray = [[NSArray alloc] initWith...];

This would be okay, because they both are being autoreleased, which would give you a retain count of 1. As, long as you release the instance variable in dealloc.

// Class Method so autoreleased
self.propertyArray = [NSArray arrayWith....];

// Explicitly declaring autorelease
self.propertyArray = [[[NSArray alloc] initWith.....] autorelease];

It's simply just a matter of preference. I prefer the first way, I think it is easier to to read and follow.

Edit (with ARC):

All these methods would be acceptable with the exception of the release statement and autorelease statement.

[tempArray release];

ARC automatically takes care of the memory management for you, so you do not have to explicitly release them. This is the benefit of ARC, you can create as many objects as you want without the headache of trying to figure out where to release them.

Also, your property would be different. You must either use strong or weak, and not retain. In this case strong would be the solution.

@property (nonatomic, strong) NSArray *tempArray;
Vikings
  • 2,527
  • 32
  • 45
  • How would this change with ARC? Can I do `self.propertyArray = [[NSArray alloc] initWith...];` or do I still have to do like the first one? – Snowman Apr 24 '12 at 14:03
0

Your first example is the way it was done before the advent of automatic reference counting (ARC). The second example works fine under ARC. Unless you have to do it the old-fashioned way, select ARC for all your projects.

0

Code like that most likely means that somebody wanted an ability to debug it easier. Basically if you have a separate variable, you can print it out in the debugger without triggering (possibly custom) property setters and getters.

SteamTrout
  • 1,684
  • 1
  • 13
  • 9