Does adding a KVO observer to self cause a memory leak?
Asked Answered
P

1

10

In a nsobject you have a property "keyPath" you want to observe itself and you use

[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:nil];

Does the above line cause a retain cycle?

I present this question because I wanted to know if it was a viable alternative to rewriting a bunch of setter functions.

Postremogeniture answered 6/4, 2014 at 6:11 Comment(0)
I
11

From the docs:

Neither the receiver, nor anObserver, are retained.

Ithnan answered 6/4, 2014 at 6:18 Comment(3)
Damn missed that part. "Discussion Neither the receiver, nor anObserver, are retained."Postremogeniture
Learned my lesson. I made the mistake of only reading the popover text when clicking on the function call and holding option/alt, if I went to the action AppleDocs html I would have seen the discussion text that would have said that. Sorry for the trouble.Postremogeniture
Because Apple doesn't take permalinks seriously, the info is still out there, but has moved here: developer.apple.com/library/archive/documentation/Cocoa/… Note: The key-value observing addObserver:forKeyPath:options:context: method does not maintain strong references to the observing object, the observed objects, or the context. You should ensure that you maintain strong references to the observing, and observed, objects, and the context as necessary.Mats

© 2022 - 2024 — McMap. All rights reserved.