问题
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