5

I don't like ARC.

But the most important feature of ARC, zeroing weak reference, is missing under non-ARC. Currently I'm using MAZeroingWeakRef, it works, but hacky, sometimes makes codes redundant. Any other ways for zeroing weak references?

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
cxa
  • 4,238
  • 26
  • 40
  • 2
    What's wrong with just nil'ing them out in Dealloc? That's all zeroing is anyhow. – CodaFi Jun 02 '12 at 03:18
  • 4
    'I don't like ARC.', we should be friends! – Jake Jun 05 '12 at 08:45
  • 4
    Your analysis here is strange. Zeroing weak references are probably the *least* important feature of ARC (nice as they are). The most important features of ARC are time and space performance improvements coupled with much safer code (fewer crashes) that also happens to be much easier to write in almost every case. What other compiler feature gives all of that? ARC is just as valuable on iOS 4 without `weak` as it is on iOS 5. http://stackoverflow.com/questions/8760431/to-arc-or-not-to-arc-what-are-the-pros-and-cons/8760820#8760820 – Rob Napier Jul 20 '12 at 18:30
  • 2
    Speaking of ARC, there is https://github.com/plausiblelabs/PLWeakCompatibility, that allows you to use __weak in non-supported ARC environments (iOS4) – Farcaller Aug 08 '12 at 10:48
  • @RobNapier dont agree... We currently work a lot with blocks but without ARC and it would solve us soooo much trouble if we had __weak refs to avoid retain cycles with blocks – Daij-Djan May 05 '13 at 08:16
  • 1
    Typically here you would use `__unsafe_unretained`. It is slightly more complex if you expect blocks to fire after their target object has been destroyed, but in practice I find this to be quite rare in well-designed code. The most common case I've seen it show up is when people have View Controllers do work that should have been in the model classes. – Rob Napier May 05 '13 at 19:57
  • Also remember that the retain loop is only a problem if it is never broken. It's ok (even beneficial) to have short-lived retain loops. So you should make sure that the object that finally fires the block makes sure to let go of it after it is done. This will break the retain loop and everything will clean-up correctly. – Rob Napier May 05 '13 at 20:02
  • 4
    What on earth is not to like about ARC? – Steve Waddicor May 12 '13 at 17:38

2 Answers2

2

Implementing zeroing weak reference is not hard. All what you have to do is just tracking all referencing pointers - store them in a collection - and assigning NULL when pointing object is being deallocated. Anyway, doing all these things manually is really a lot of work, you literally need to write all manual tracking code to be efficient enough in Objective-C.

And at the end, you will finally discover you need some automatic code writing machine - static compiler - and that's exactly what ARC does. You could implement something like ARC yourself. But If I am you, I will just use already existing, robust, stable, well-designed and supported implementation by compiler maintainer.

Also, not following Apple is not wise behavior if you want to develop Apple-stuffs. Unlike other platform holders - such as Microsoft -, Apple doesn't care much about keeping backward compatibility. If they don't think something is good, it will be deprecated and removed eventually - like Objective-C GC.

eonil
  • 83,476
  • 81
  • 317
  • 516
1

I think you should stick to the paradigm Apple itself recommended before ARC was introduced, that is, nil-ing all of your "weak" references from within the -dealloc method. Not only it is a de facto standard, but it also is the way the Xcode code refactor behaves when ARC is not enabled, so conforming to it should save you a couple headaches.

Michele De Pascalis
  • 932
  • 2
  • 9
  • 26