Inconsistent object deallocation with ARC?

前端 未结 2 947
独厮守ぢ
独厮守ぢ 2021-02-06 06:08

I was playing around with memory (de)allocation stuff on a simple command line app for Mac OSX 10.7 built using Xcode Version 4.2.1 with ARC enabled, and the default build setti

2条回答
  •  遥遥无期
    2021-02-06 06:30

    If you disassemble A.R.C.-produced code, every access to a weak variable is wrapped in a call to this function:

    id objc_loadWeak(id *location)
    {
        return objc_autorelease(objc_loadWeakRetained(location));
    }
    

    This checks if the object has already been dealloced, and if not, retains and autoreleases it, for extra safety against premature deallocs.

    Therefore in your third example, the early calls to method on weakRef are causing its retain count to be increased, so nilling your pointers doesn't cause it to be dealloced.

提交回复
热议问题