Under automatic reference counting, why are retain, release, and dealloc not allowed?

前端 未结 4 1612
忘掉有多难
忘掉有多难 2020-11-28 10:08

When trying to use -retain, -release, and -dealloc while building my application using automatic reference counting in Xcode 4.2, I ge

4条回答
  •  离开以前
    2020-11-28 10:51

    Basically:

    When using ARC, it's all or nothing. Either the compiler is managing all of the retains/releases/deallocs for you, or it is doing nothing. You cannot intersperse your own calls to them, because the compiler wants to do it all itself. It can make absurd optimizations by doing this (for example, a method that returned an autoreleased object under Manual Memory Management may now produce an object that never ends up in an autorelease pool). If you were to start sprinkling in your own calls to retain and release, then the compiler would have to work with these and wouldn't be able to perform a lot of the optimizations that it wants (and that you should want).

    And as an added bonus, invoking -retainCount is now a compiler error! OH HAPPY DAY!

提交回复
热议问题