1

I have started converting my projects to work under ARC and I was wondering how the following would behave.

As I understand the following line would cause a memory leak under the manual memory management rules.

self.array = [[NSArray alloc] init];

and it is recommended to use an autorelease object such as ,

  self.array = [NSArray array] or
  array = [[NSArray alloc] init]; 
  1. Therefore, does the ARC mode cause a memory leak from the following line as well?

    self.array = [[NSArray alloc] init];

  2. When we are directly assigning to the array(?) as follows without using the generated setter could this cause premature release of the array ?

    array = [[NSArray alloc] init];

Please consider array as an instance variable.

4

2 に答える 2

3

1) NO, not leaking.
2) NO, should work as well

Both ways are safe with ARC. Anyhow you should use properties where possible. The only case you need to be aware of is the following:

If your property is weak and you assign a newly created object like self.array = [[NSArray alloc] init], it will be gone right in the next line. That's a little bit strange in ARC. But if your properties are strong, you don't need to care about memory stuff at all.

于 2012-11-05T17:30:32.823 に答える
1

When you use ARC, the compiler actually inserts the proper Retains and Releases for you, so you won't have to worry about memory leaks in these situations. It helps to read a bit on ARC from the "Transitioning" guide:

http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

于 2012-11-05T17:30:42.193 に答える