NSPointerArray weird compaction

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:37:26

I've seen this same behavior. Here is an open radar bug report: http://www.openradar.me/15396578

The reason this happens is that -compact first checks whether an internal flag 'needsCompaction' is set. If it's not, it simply bails early. The only time the flag is set is if a nil pointer is inserted directly into the array through the public interface. It does not get set if a weakly referenced object is deallocated (and the pointer is nil'd) after the pointer was inserted into the array.

One work around for this behavior is to purposefully append a nil pointer to the array before calling -compact. Not ideal but it'll work.

[pa addPointer:nil]; // forces the pointer array to do compaction next time
[pa compact];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!