Search code examples
objective-cmemory-managementraiireference-counting

shared_ptr / weak_ptr implementations for objective-C


Noticing how badly implemented is reference counting in current Objective-C (see here and here), i'm sure there must be a library out there providing something similar to c++ shared_ptr and weak_ptr semantics without all those ridiculous extra calls to retain and release that should be called automatically when (pointer) variables go out of scope

Is this possible on ObjC? I know that ObjC does not have destructors so there is no way to have something called automatically when variables go out of scope, but how can these retain/release calls be really necessary? am i going around this in the wrong way?


Solution

  • That is taken care of by ARC. Other than ARC, no there really isn't a good way of doing it. In C++, you have stack objects and you can overload assignment (=) and deference (->). Both are required for shared pointers. Niether can be done in objective C.