1

~ Will ARC always release an object the line after the last strong pointer is removed? Or is it undetermined and at some unspecified point in the future it will be released? Similarly, assuming that you don't change anything with your program, will ARC always be the same each time you run and compile your program?

~ How do you deal with handing an object off to other classes? For example, suppose we are creating a Cake object in a Bakery class. This process would probably take a long time and involve many different methods, so it may be reasonable for us to put the cake in a strong property. Now suppose we want to hand this cake object off to a customer. The customer would also probably want to have a strong pointer to it. Is this ok? Having two classes with strong pointers to the same object? Or should we nil out the Bakery's pointer as soon as we hand off?

jscs
  • 63,694
  • 13
  • 151
  • 195
Nosrettap
  • 10,940
  • 23
  • 85
  • 140
  • This should really be two separate posts. The two bullet points aren't very closely related. – jscs Jun 20 '12 at 17:45

5 Answers5

3
  1. Your code should be structured so the answer to this doesn't matter - if you want to use an object, keep a pointer to it, don't rely on ARC side effects to keep it around :) And these side effects might change with different compilers.

  2. Two strong pointers is absolutely fine. ARC will only release the object when both pointers are pointing to something else (or nothing!)

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
0

Ok, first this answer might helpt you also a little bit: ARC equivalent of autorelease?

Generally after the last strong variable is nilled, the object is released immediately. If you store it in a property, you can nil the property, assign it to something like __strong Foo *temp = self.bar; before you nil, and return that local __strong variable (although arc normally detects the return, and inferes the __strong byitself).

Some more details on that: Handling Pointer-to-Pointer Ownership Issues in ARC

Community
  • 1
  • 1
Joerg Simon
  • 421
  • 2
  • 7
0
  1. ARC will implement the proper retains and releases at compile time. It will not behave any different than if you put them in there yourself so it will always do the same compilation and to answer your question should always behave the same. But that said it does not mean that your object will always be released immediately after the pointer is removed. Because you never call dealloc directly in any form of objective C you are only telling it that there is no reference count and that it is safe to release. This usually means that it will be released right away though.

  2. If you pass an object from one class to another and the receiving class has a strong property associated with it and the class that passes it off eventually nils its pointer it will still have a reference count of at least 1 and will be fine.

rooster117
  • 5,502
  • 1
  • 21
  • 19
0

DeanWombourne's answer is correct; but to add to (1).

In particular, the compiler may significantly re-order statements as a part of optimization. While method calls will always occur in the order written in code (because any method call may have side effects), any atomic expression may be re-ordered by the compiler as long as that re-order doesn't impact behavior. Same thing goes for local variable re-use, etc...

Thus, the ARC compiler will guarantee that a pointer is valid for as long as it is needed, no more. But there is no guarantee when the pointed to object might be released other than that it isn't going to happen beyond the scope of declaration. There is also no guarantee that object A is released before B simply because A is declared and last used before B.

IN other words, as long as you write your code without relying on side effects and race conditions, it should all just work.

bbum
  • 162,346
  • 23
  • 271
  • 359
-1

Please keep you code proper as it has diffrent behaviour on diffrent complier.

enter image description here