2

I wonder how memory management must be done for method parameters that are used to pass values into the method and (optionally) return updated values from the method.

For example, the -validate<Key>:error: method in KVC has a (id *)ioValue parameter that can be updated by the method. You can return an autoreleased object but what should you do with the input object when updating? Are there any conventions for this case?

jscs
  • 63,694
  • 13
  • 151
  • 195
yurish
  • 1,515
  • 1
  • 15
  • 16
  • If you are updating an object, rather than creating one, then there are no memory management issues are there? The object has been allocated and you are modifying one of its properties. – trojanfoe May 21 '13 at 07:15
  • I am thinking about different scenario when you completely replace the input object. Just updating the input object would mean that your object is passed as _in_ argument not as _in-out_ . Take a look at -validate:error: or validateValue:forKey:error: method description of NSKeyValueCoding protocol. – yurish May 21 '13 at 07:27
  • 1
    @yurish - see this question for a detailed look at this issue: http://stackoverflow.com/questions/8814718/handling-pointer-to-pointer-ownership-issues-in-arc – CRD May 22 '13 at 03:50

1 Answers1

1

There isn't anything particularly complex about replacing an object reference with a new object, just as taking a nil reference and making it valid when an object is first created.

If you are using ARC then the compiler will realise that the old reference has been replaced, just like it would in any other situation. If using MRR then you are required to observe the normal retain/release conventions in order to avoid a memory leak.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • I had to understand this myself :) So you just must be extra careful when calling such method and assume that the argument you passed in may be changed. – yurish May 21 '13 at 08:07