Giter Site home page Giter Site logo

nsdeallocnotifier's Introduction

一个 NSMapTable strong-weak释放的可行性验证方案

NSMapTable 的 weak 自动释放并不像 Apple 描述的那么可靠(弱引用归零存在问题,并不符合官方描述的预期,无论是weak-strong还是strong-weak亦或是其他类型),所以参考了一些思路,实现了一个可以令其稳定释放的方案

核心思路:

  • 创建一个自定义类 NSDeallocNotifier,该类在实例化构造时接收一个 block 作为参数,并在该类析构 (deinit/dealloc) 时执行该 block.
  • 在需要 NSMapTable 添加对象时,实例化一个 NSDeallocNotifier,并在其回调块中执行 NSMapTable 的 remove 方法来删除其中的键/值对,也就是说在 NSDeallocNotifier 释放时,同时释放掉 NSMapTable 中的键/值对.
  • 将实例化的 NSDeallocNotifier 以关联对象的方式,添加到键/值对上。保证当 weak 持有的键/值对象被 deinit/dealloc 时,所关联的 NSDeallocNotifier 也将被 deinit/dealloc,从而会触发 block,也就会跟着及时释放 NSMapTable 中的键/值对。

nsdeallocnotifier's People

Contributors

shevakuilin avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.