Get all existing pointers to an object

荒凉一梦 提交于 2019-12-24 01:56:31

问题


Is it possible to get a list of pointer to a pointers to an objective c object.

something like

id **pointers(id object, int *out_count)

Pretty crazy, huh? =)


回答1:


Unfortunately, no. If such a thing were generally possible, then writing a precise garbage collector would be rather simple:

int count;
pointers(obj, &count);
if (count == 0) {
    free(obj);
}

Since the objective-c garbage collector has to chase pointers from roots, control the allocator, and scan the stack conservatively to achieve something like this, I think it's reasonable to assume that you'd need to do the same.

It might be possible to leverage the garbage collector's implementation of this, though, if running in GC mode. Not a good idea, not simple, and won't work on iOS, but maybe possible. libauto is open source after all.



来源:https://stackoverflow.com/questions/8600922/get-all-existing-pointers-to-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!