Search code examples
iosobjective-cpopviewcontrolleranimated

popViewControllerAnimated: doesn't release view controller from memory


I have two view and second view is pushed from first view. For testing, I go to second view from first view and then I go back to first view. After that, I send nsnotification and in my second view, it receive my notification.

1) May I know why I receive notification in second view after it is pop up? For IBOutlets, I declare weak property also.

2) If it is still in memory, for other data like nsdictionary, nsstring, shall I use strong or weak property? Will those also in memory?

3) If I don't want my second view in memory totally, how shall I do?


Solution

  • If your second view controller is not released when you "go back" to the first view, then either

    • You are not really "going back" - you are accidentally creating a new first view controller and pushing it, which is unlikely; or:

    • You have a retain cycle in your second view controller.

    I'm betting that you do have a retain cycle. You should try to track this down. You mention notifications: it is very easy to create an accidental extra retain when setting up a view controller as a notification observer, so that's probably the cause.

    In particular, see my book's discussion of this topic:

    • If you called addObserverForName:object:​queue:usingBlock:, you will leak (under ARC) unless take elaborate precautions (such as doing the weak-strong dance in the block, to avoid a strongly retained self).