-1

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?

Community
  • 1
  • 1
lurscher
  • 25,930
  • 29
  • 122
  • 185

2 Answers2

9

Noticing how badly implemented is reference counting in current Objective-C...

I take issue with that. It's not badly implemented. It's just that you're unfamiliar with it. I think it's really straight-forward, because there are only a couple of rules you have to know. So don't knock it just because it's unfamiliar to you.

Is this possible on ObjC?

Yes, this is possible. It's called ARC ("Automatic Reference Counting"). See the "Transitioning to ARC Release Notes" for more information.

I know that ObjC does not have destructors

Actually, it does. That's what the -dealloc method is.

...when variables go out of scope

The thing to understand about Objective-C is that objects do not go out of scope, since objects can only be allocated on the heap (<insert normal caveat here about non-copied blocks>). Pointers to those objects (i.e., variables) can go in and out of scope, but the objects themselves are always in scope, because the heap technically counts as being global in scope. (It's just that if you lose the address of an object, it's nigh on impossible to find it again)

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • thanks for your answer. On the issue that variables can't go out of scope because they are allocated on heap. Yes But, the pointer themselves like on the stack. The reference tracking cleaup (done by `release`) should be called when those stack pointers go out of scope. Sorry i recognize that i'm unfamiliar with the objC memory management model, but i think my criticisms should be shared by anyone with experience with reference counting implementations on languages where RAII idiom is possible – lurscher May 27 '12 at 01:29
  • 1
    @lurscher yes, the pointers are on the stack. But in C (on which Objective-C is built), a pointer is just a number that happens to be interpreted as a memory address. And from my crude understanding of RAII, it seems like it's only because the language is built around exception-based control flow, right? (to guarantee that destructors run) If that's the case, then it's moot, because Objective-C *really* discourages exception-based control flow. But as I said, ARC is the compiler trying to do what you want for you. It works really well. – Dave DeLong May 27 '12 at 02:24
  • i understand that pointers are just values, but the compiler has the knowledge required for adding the release messages when the pointer variables go out of scope, it just chooses to leave that burden to the programmer. The worst part is that it seems to be a burden that cannot be properly hidden as an implementation detail of a library pointer object. Also, RAII is way broader than a mere mechanism for releasing resources when exception happens, but that is outside the scope of the current question to discuss – lurscher May 27 '12 at 03:19
  • 2
    @lurscher Stack Overflow is for getting answers about the way things are, not for discussing how you think they ought to be. Join a mailing list for that. – benzado May 28 '12 at 17:46
2

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.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117