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];
-
Therefore, does the ARC mode cause a memory leak from the following line as well?
self.array = [[NSArray alloc] init]; -
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.
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.