objective c - Validity of these pointers -


i trying improve grasp on pointers , arc. consider following example

@implementation foobar  -(nsarray *)methoda {     return self.somearray; }  -(nsarray *)methodb {     return [[nsarray alloc] init]; }  @end 
@interface foo : nsobject @property(strong) nsarray * myarraya; @property(strong) nsarray * myarrayb; @end  @implementation foo  -(void)foomethod {     //suppose f instance of foobar     self.myarraya = [f methoda];  //---->statement 1     self.myarrayb = [f methodb];  //---->statement 2     //destructor of f instance called.     //will myarraya , myarrayb valid ? }  @end 

now in foomethod class foo has 2 strong properties of arrays myarraya , myarrayb myarraya referencing strong pointer class instance foobar myarrayb referencing strong pointer created in scope . suppose somehow destrcutor of instance f called. when destrcutor called clear contents of of somearray invalidating myarraya since destructor not aware of memory allocated methodb memory still alive. understanding if instance f destroyed after statement 2 address pointed myarraya invalid whereas address pointed myarrayb valid. kindly let me know if understanding correct.

in objective-c, method called when object ceases exist called dealloc, , tend refer that, dealloc, , don't call "destructor", although dealloc pretty close equivalent of other language's destructor.

however, said, arc forbids directly calling dealloc on object. if calling dealloc on object directly, project not compile if using arc.

instead, arc writes in memory management code. @ point after object has 0 remaining strong references (it not guaranteed happen immediately), arc deallocate object. importantly, object can have any number of strong references it. so, let's take @ specific example.

f, in example, instantiated instance of class foobar has property somearray. fail show how property declared, let's assume it's declared same way properties in foo declared (as strong). @ whatever point somearray property becomes non-nil, there @ least 1 strong reference array.

for sake of argument, let's assume f strong reference array. make array's reference count equal one.

now, in foo, set myarraya f's somearray property. array has 2 strong references--a reference count equal two.

meanwhile, foo's myarrayb set equal newly instantiated nsarray object. f doesn't keep strong reference array. methodb returns, foo class's myarrayb has strong reference array. has reference count equal one.

now, let's keep f around (through whatever means), our foo instance deallocated (however), happens 2 arrays?

because our instance of foo deallocated, no longer hold strong reference either array. arc decrement each array's reference count one.

foo's myarraya have reference count decremented 2 one--f still holds strong reference, , array not deallocated.

foo's myarrayb have reference count decremented 1 zero--nothing else holds strong reference myarrayb, reference count of zero, arc deallocate array.