0

I have a question about how best to manage array pointers to ensure no memory leaks happen. I have a container class A and a composited class B. Both have an array property, and both do their own thing to the array. But the container is the only class exposed to the public API. so I set classA.someProperty and it internally sets classB.someProperty like below. What kind of cleanup work do I need to do? Will ARC take care of this for me automagically?

class A

 @property(nonatomic,strong) NSMutableArray* someProperty;
 @property(nonatomic,strong) ClassB* classB;


Class B

 @property(nonatomic,strong) NSMutableArray someProperty;

and in the implementation in Class A;

classB.someProperty = [self.someProperty mutableCopy]
//do some other work with self.someProperty

and in the implementation in Class B;
//do some work with self.someProperty [Includes modifications to the array]
user2453876
  • 303
  • 2
  • 12
  • If you set all `strong` properties to `nil` in `- dealloc`, it should be fine. That's the point of reference ***counting.*** –  Jun 18 '13 at 18:11
  • Are you sure about setting properties to nil in dealloc - http://stackoverflow.com/questions/7906804/do-i-set-properties-to-nil-in-dealloc-when-using-arc – user2453876 Jun 18 '13 at 18:13
  • @userXXX Yes I am. Seen that rant before. BS, at best. –  Jun 18 '13 at 18:14
  • I must add, I am using ARC. Isnt everyone using ARC these days? – user2453876 Jun 18 '13 at 18:14
  • @userXXX No, for example I don't. –  Jun 18 '13 at 18:15
  • 2
    Dont mean to offend you, but do you have any reference that recommends setting all strong properties to nil in dealloc when using ARC? – user2453876 Jun 18 '13 at 18:16
  • @user2453876 I don't use it as well. Yes, always nil out... otherwise you can access the value... It's still reachable, unless something else doesn't take its place. – tyrhus Jun 18 '13 at 18:17
  • @user2453876 I don't have any reference at my mind, but you still have to implement `- dealloc` under ARC. –  Jun 18 '13 at 18:21
  • @tyrhus it's not about the "access" - it's about its reference count. Ownership of properties is ought to be relinquished upon deallocation. –  Jun 18 '13 at 18:22
  • 3
    @H2CO3 you absolutely don't *have* to implement dealloc and to my knowledge, you also don't have to nil properties under ARC. Where do you get that from? – Mario Jun 18 '13 at 18:24
  • @Mario For example, [this](http://stackoverflow.com/questions/7292119/custom-dealloc-using-arc-objective-c). You have to implement `dealloc`, it's just that `[super dealloc]` is called implicitly. –  Jun 18 '13 at 18:27
  • To my knowledge, you should NOT nil properties in dealloc. Please lookup the detailed answer in . – Reinhard Männer Jun 18 '13 at 18:29
  • @H2CO3: Sorry, I suddenly was not sure, what your opinion was, so I deleted my answer addressed to you, and formulated it more general, see above. – Reinhard Männer Jun 18 '13 at 18:31
  • @ReinhardMänner No problem, will do the same. –  Jun 18 '13 at 18:31
  • @ReinhardMänner I may be wrong but the statement "never use setters in dealloc" statement is wrong too. I actually downvoted that answer back then. [link](http://stackoverflow.com/questions/7906804/do-i-set-properties-to-nil-in-dealloc-when-using-arc#comment12191515_7906891) - "You can actually create new problems by not using the setter". –  Jun 18 '13 at 18:32
  • 3
    There is no need to implement dealloc. Right from Apples docs: "you may implement a dealloc method if you need to manage resources other than releasing instance variables." (http://developer.apple.com/library/ios/ipad/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html) - they also say nothing about having to nil properties (except non-ARC delegates) – Mario Jun 18 '13 at 18:35

1 Answers1

1

You don't need to write any cleanup code, ARC will take care of releasing the memory. ARC will automatically put the code to decrement the reference count for variables defined as strong properties.

I would recommend you think in terms of reference count for a variable to understand the memory management. ARC will automatically insert the statements to decrement the reference count at proper places. Lets consider two scenarios for your example:

Scenario 1

No other class has a variable referencing class A's someVariable: when class A reference counts becomes 0 then you can be sure that ARC has put the code to make someVariable reference count 0 (in class A's dealloc).

Scenario 2

Another variable (say in Class C) that is referencing Class A's someVariable then reference count of someVariable will still be decremented (by the code inserted by ARC) but it won't be zero so it won't be released (and that is what you want because you want to access it in Class C).

rakmoh
  • 2,953
  • 1
  • 14
  • 14