ios - Properties and ARC -
i working arc , cocos2d 2.0 static library (which not use arc , compiled separate target). translated old project (made without arc) , wondering whether declaring properties in way has potential retain cycle issue:
@interface playerdata : nsobject <nscoding> { } //is ok save if game gets paused may want save this. @property (readwrite, nonatomic) int numberofhits; @property (readwrite, nonatomic) bool everbeenhitincurrentlevel; @property (readwrite, nonatomic) int hitsforonehearth;
i noticed various scenes build memory on time. added cclog call in release method of cclayer method (myscene : cclayer) , never gets called. that's how create scene (which replace using "[ccdirector shareddirector] replacescene" method
+ (id) scenewithlevelname:(levelname)name { ccscene *scene = [ccscene node]; shooterscene * shooterlayer = [[self alloc] initwithid:name]; [scene addchild:shooterlayer]; return scene; }
edit: realized stupidly did not include example objects , used primitive data type here paste snippets of elements in scenes: characterselection, shooterscene , planetselectionmenu:
//shooterscene @interface shooterscene : cclayer { hudlayer * hudlayer; .... } @property(readwrite, nonatomic) ccspritebatchnode* backgroundandenemiesbatchnode; @property(readwrite, nonatomic) shipentity* playership; ... etc.. please note not declare member variables properties playership , backgroundandenemiesbatchnode beause, far can understand, should suffice property declaration (but please correct me if wrong or if approach may cause issues). //characterselectionscene @interface characterselectionscene : cclayer { int currentlyselectedcharacterspritetag; ccsprite * lights; cclayer * spriteslayer; ... } //planetselectionmenu #import <foundation/foundation.h> #import "cocos2d.h" @interface planetselectionmenu : cclayer { cclayer * backgroundlayer; // added background images here cclayer * alwayspresentitems; } + (id) scene; @end
please note each time go planetselectionmenu characterselectionscene -and vice versa- memories increases. in case have not used properties "just" added objects (ccsprites) layers , batchnodes.
edit 2: here see in allocation when running through menu->characterselection->planetselectionscene etc.. seem on avarage livebytes 4mb and, see 1 scene @ time assume there no retain cycles. why nasty low memory messages?
although default property attribute strong
, can't properties causing retain cycle primitive types , default assign
three other common methods of introducing retain cycles jump mind:
- are implementing delegate pattern anywhere (
@protocol
s). delegate's weak references necessary?
-(id) initwithdelegate:(id) target { ... _target = target; //_target should weak reference ... }
- do of children nodes reference parents?
-(id) initwithparent:(ccnode*) parentnode { ... _parent = parentnode; //_parent should weak reference. ... }
- do blocks reference
self
^{[self callsomemethod];}
should use weak reference self
:
__weak typeof(self) selfreference = self; ^{[selfreference callsomemethod];}
i find best way find leaks arc not use leaks tool, allocations tool. since of scenes tend have word "scene" in symbol, filter word scene.
since using replacescene
should have 1 scene alive @ time (excepting during transition), should see 1 object in object summary.
if have scene that's hanging around, find it's best @ objects retain history. here, pair each retain corresponding release until find culprit(s) retaining , not releasing scene. more not it's obvious retain cycle block, or property declared strong instead of weak.
Comments
Post a Comment