objective c - Are unreachable objects safe from collection for any time after becoming unreachable? -
i'm storing obj-c objects in c++ data structure. since i'm running under garbage collection , objects reachable via c++ structure, i'm calling cfretain() root each object added structure ensure aren't collected prematurely:
- (void) dosomethingfancywithobjects:(nsarray*)array { std::list<nsobject*> list; (nsobject* obj in array) { id copyaddedtolist = [obj copy]; list.push_back(copyaddedtolist); cfretain(copyaddedtolist); // otherwise list.back() becomes unreachable... } // ... // boost_foreach(nsobject* obj, list) { cfrelease(obj); } }
is necessary this? there chance gc kick in , collect unreachable objects during method in become unreachable? can gc collect @ time, or @ specific times such end of run loop? haven't managed find relevant bit of documentation on this.
as posted in code in happens within scope of dosomethingfancywithobjects:
not need cfretain/cfrelease
pair because not removing objects array
on stack , therefore rooted.
edit
ok, didn't read code - objects being copied.
firstly, question need copying. if modifying objects in list, why? being thrown away @ end.
secondly, have race condition. garbage collector in between list.push_back([obj copy]);
, cfretain(list.back());
, object pointer in std::list
dangling. should like:
nsobject* thecopy = [obj copy]; cfretain(thecopy); list.push_back(thecopy);
Comments
Post a Comment