ios - Using self and self properties inside a block in non-arc environment -



ios - Using self and self properties inside a block in non-arc environment -

i have been using blocks , familiar memory management when using self within blocks in non-arc environment

but have 2 specific questions:

1) understand can utilize __block avoid retain cycle retained block in turn using self can create, below:

__block myclass *blockself = self; self.myblock = ^{ blockself.someproperty = abc; [blockself somemethod]; };

this avoid retain cycle sure doing have created scope self released , deallocated else. when happen self gone , blockself pointing garbage value. there can conditions when block executed after self deallocated, block crash trying utilize deallocated instance. how can avoid condition? how check if blockself valid when block executes or stop block executing when self deallocated.

2) on similar lines suppose utilize block below:

__block myclass *blockself = self; self.myblock = ^{ [blockself somemethod:blockself.someproperty]; }; // taking someproperty in method argument -(void) somemethod:(datatype*)myclassproperty { myclassproperty = abc; }

now there can situations self not released someproperty released before somemethod's execution starts (this happen when there multiple threads). if self.someproperty = nil; when release, myclassproperty not nil , pointing garbage, hence when somemethod executed first line lead crash. how avoid this?

this same issue non-zeroing weak references everywhere else in non-arc code, e.g. delegates etc. (mrc doesn't have zeroing weak references; these kind of weak reference in mrc. yet people still able write safe code in pre-arc days.)

basically, solution need clear map of ownership. either self responsible keeping block alive; or other object responsible keeping block alive.

for example, delegates, usually, "parent" object delegate of "child" object; in case "parent" object responsible keeping "child" object alive, "parent" object outlive "child" object, reference can weak , safe (because kid object's methods perchance called parent object while parent object alive).

on other hand, if have asynchronous operation, , block given operation callback, operation responsible holding onto block. in case, self not hold onto block. , block hold strong reference self, when operation done, can still safely whatever needs on self. in fact, whatever object self is, doesn't need retained whoever uses it, since indirectly retained asynchronous operation -- can create, fire, , forget kind of thing.

if have block kept live self, , kept live else, should re-think design. "there can conditions when block executed after self deallocated"; well, should describe whole design , how can happen. because usually, executed asynchronously, self need not hold onto block.

your code confusing , doesn't create sense. example, why assign parameter (myclassproperty)? what's point of passing argument when parameter going overwritten anyway? why name local variable "myclassproperty"?

what think asking accessing property can changed on different threads, , how deal memory management of that. (this question unrelated blocks or arc/mrc. as issue in arc.)

the reply need atomic property. can create synchronized property atomic, or implement atomic property manually if know how. atomic property of object pointer type needs getter needs not homecoming underlying variable, retain , autorelease it, , homecoming result of that. retrieval , retaining in getter occurs in critical section synchronized critical section in setter includes release of old value , retaining of new. basically, synchronization guarantees value not released in setter in between retrieving value , retaining in getter. , value returned getter retained , autoreleased, guaranteed live duration of scope.

ios objective-c iphone objective-c-blocks self

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -