ios – NSHashTable保留弱参考

ios – NSHashTable保留弱参考,第1张

概述因此,Apple的[NSHashTable weakObjectsHashTable]文档指出: Returns a new hash table for storing weak references to its contents. 所以,当我运行以下代码时…… NSHashTable *hashTable = [NSHashTable weakObjectsHashTable];ABC * 因此,Apple的[NSHashtable weakObjectsHashtable]文档指出:

Returns a new hash table for storing weak references to its contents.

所以,当我运行以下代码时……

NSHashtable *hashtable = [NSHashtable weakObjectsHashtable];ABC *object = [[ABC alloc] init];[hashtable addobject:object];NSLog(@"%@",[hashtable anyObject]);object = nil;NSLog(@"%@",[hashtable anyObject]);

输出是:

2017-09-18 12:57:02.801 Test2[6912:640614] <ABC: 0x6    

Returns a new hash table for storing weak references to its contents.

014eb0>2017-09-18 12:57:02.801 Test2[6912:640614] <ABC: 0x6

Returns a new hash table for storing weak references to its contents.

014eb0>2017-09-18 12:57:02.803 Test2[6912:640614] dealloc // (dealloc of ABC)

它看起来像调用NSLog(@“%@”,[hashtable anyObject]);保留对象.

如果我打电话,

NSHashtable *hashtable = [NSHashtable weakObjectsHashtable];ABC *object = [[ABC alloc] init];[hashtable addobject:object];//NSLog(@"%@",[hashtable anyObject]);

产量如预期:

2017-09-18 13:00:23.949 Test2[6936:645459] dealloc2017-09-18 13:00:23.949 Test2[6936:645459] (null)

谁能告诉我我的误解在哪里?

解决方法 您甚至不必调用NSLog – 只需执行[hashtable anyObject]即可使对象保持不变.原因似乎是anyObject返回一个自动释放的引用,它不会被释放:d直到将来的某个点.将整个事物包装在@autorelease块中,我相信它会按预期工作.

一般来说,确定ARC:ed对象何时会被释放是很危险的,因为有很多这样的魔法.

总结

以上是内存溢出为你收集整理的ios – NSHashTable保留弱参考全部内容,希望文章能够帮你解决ios – NSHashTable保留弱参考所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1075391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存