zoukankan      html  css  js  c++  java
  • autoreleasing on a thread

    So basically, if you are running on OS X 10.9+ or iOS 7+, autoreleasing on a thread without a pool should not lead to a leak. This is not documented and is an internal implementation detail, so be careful relying on this as Apple could change it in a future OS. However, I don't see any reason why they would remove this feature as it is simple and only has benefits and no downsides, unless they completely re-write the way autorelease pools work or something.

    The Objective-C runtime is open-source so you can read the source to see what's going on. The latest version of the runtime (646, which shipped with OS X 10.10 and iOS 8) does indeed add a pool if you perform an autorelease without a pool on the current thread. In NSObject.mm:

    static __attribute__((noinline))
    id *autoreleaseNoPage(id obj)
    {
        // No pool in place.
        assert(!hotPage());
    
        if (obj != POOL_SENTINEL  &&  DebugMissingPools) {
            // We are pushing an object with no pool in place, 
            // and no-pool debugging was requested by environment.
            _objc_inform("MISSING POOLS: Object %p of class %s "
                         "autoreleased with no pool in place - "
                         "just leaking - break on "
                         "objc_autoreleaseNoPool() to debug", 
                         (void*)obj, object_getClassName(obj));
            objc_autoreleaseNoPool(obj);
            return nil;
        }
    
        // Install the first page.
        AutoreleasePoolPage *page = new AutoreleasePoolPage(nil);
        setHotPage(page);
    
        // Push an autorelease pool boundary if it wasn't already requested.
        if (obj != POOL_SENTINEL) {
            page->add(POOL_SENTINEL);
        }
    
        // Push the requested object.
        return page->add(obj);
    }
  • 相关阅读:
    CDZSC_2015寒假新人(4)——搜索 A
    第一次组队赛---2010年全国大学生程序设计邀请赛(福州)L
    ZSC新生赛 沼跃鱼早已看穿了一切
    ZSC新生赛 聪明的员工
    CDZSC_2015寒假新人(1)——基础 I
    Linux 系统时间和硬件时间
    Python 深浅复制
    Python 函数内省
    Python 函数参数
    Python 可调用对象
  • 原文地址:https://www.cnblogs.com/feng9exe/p/7240897.html
Copyright © 2011-2022 走看看