objective c - Need help wih references - ARC + Cocos 2d -
arc kicking arc
i installed cocos 2d , unhappy seeing wanted use arc. having issue can't quite seem figure out.
instruments saying have 5 references graphicsmanager object, yet can conclude 4 of them. need create , release many of these.
class "rowmanager" (cclayer) calls class "graphicsmanager" (ccnode) make these objects. rowmanager calls methods move objects via selector: [self schedule:@selector(row:) interval:.01]
finally, when object arrives @ point, method "row1finished" called creates 1 more reference object before calling object (die) remove it's own children.
i can 3 animations stop, object detach rowmanager , disappear, yet dealloc method still not call itself. doing wrong?
to convert non-arc cocos2d template followed instructions here:
http://www.learn-cocos2d.com/2012/04/enabling-arc-cocos2d-project-howto-stepbystep-tutorialguide/
///////////////// @interface rowmanager : cclayer { graphicsmanager *graphicsobject; } @implementation rowmanager -(id) init { if( (self=[super init]) ) { graphicsobject = [[graphicsmanager alloc] initwithboattype:@"flatboat" withword:@"life" usingcharacter:@"puchai" orientation:@"up" visible:yes]; graphicsobject.tag = 21; graphicsobject.position = ccp(offset,size.height/4.1); [self addchild: graphicsobject]; [self schedule:@selector(row1:) interval:.01]; [self schedule:@selector(rotate:) interval:6]; // etc… -(void)row1:(cctime)dt{ // scroll object across screen graphicsobject = (graphicsmanager *)[self getchildbytag:21]; // reference: row method graphicsobject.position = ccp(graphicsobject.position.x-1, graphicsobject.position.y); if (graphicsobject.position.x < 0 - graphicsobject.boatwidth){ [self row1finished]; } } -(void)rotate:(cctime)dt { //waggle object bit graphicsobject = (graphicsmanager *)[self getchildbytag:21]; // reference 2: rotate method [graphicsobject runaction:[ccsequence actions: [ccrotateby actionwithduration:1 angle:.8], [ccrotateby actionwithduration:2 angle:-.8], [ccrotateby actionwithduration:1 angle:-.8], [ccrotateby actionwithduration:2 angle:.8], nil]]; } -(void) row1finished { graphicsobject = (graphicsmanager *)[self getchildbytag:21]; // reference 3: reference referenced [self unschedule:@selector (row1:)]; [self unschedule:@selector(rotateboat:)]; [graphicsobject die]; // call within object class remove references [self removechild:graphicsobject cleanup:yes]; graphicsobject = nil; } ///////////////////// in graphics manager class: /////////////////// @interface graphicsmanager : ccnode {...} @property cgfloat boatwidth; …etc @implementation graphicsmanager @synthesize boatwidth; - (void) die { [self unschedule:@selector(puchairow:)]; // reference 4 animation [self stopallactions]; [self removeallchildrenwithcleanup:yes]; // removes sub objects created in class nslog(@"died"); } - (void)dealloc { cclog(@"graphics manager dealloc: %@", self); } -(id)initwithboattype:(nsstring *)boattype orientation:(nsstring *)orientation { self = [super init]; if (self){ if ([orientation isequal: @"upstream"]){ flatboat = [ccsprite spritewithfile:@"flatboat.png"]; flatboat.anchorpoint = ccp(0,0); self.boatwidth = [flatboat boundingbox].size.width; [self addchild:flatboat]; [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"puchai.plist"]; puchai = [ccsprite spritewithspriteframename:@"puchai1.png"]; puchai.anchorpoint = ccp(0,0); puchai.position = ccp(20,30); puchai.tag = 4; [self addchild:puchai]; [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"cube.plist"]; cube = [ccsprite spritewithspriteframename:@"cube1.png"]; cube.scale = .80; cube.tag = 56; cube.visible = no; cube.position = ccp(150,45); [self addchild:cube]; cube = [ccsprite spritewithspriteframename:@"cube2.png"]; cube.scale = .80; cube.position = ccp(-1 +150,[cube boundingbox].size.height +45); cube.tag = 57; cube.visible = no; [self addchild:cube]; [self schedule:@selector(puchairow:) interval:3]; // reference 4 animation flatboatwing = [ccsprite spritewithfile:@"flatboatwing.png"]; flatboatwing.anchorpoint = ccp(0,0); flatboatwing.position = ccp(25,17); [self addchild:flatboatwing]; } ///etc -(void)puchairow:(cctime)dt{ puchai = (ccsprite *)[self getchildbytag:4]; [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"puchai.plist"]; nsmutablearray *frames = [[nsmutablearray alloc]init]; (int = 1; i<=8; i++) { nsstring *framename = [nsstring stringwithformat:@"puchai%i.png",i ]; [frames addobject:[[ccspriteframecache sharedspriteframecache] spriteframebyname:framename]]; } ccanimation *a = [ccanimation animationwithspriteframes:frames delay:1.0f/5.0f]; [puchai runaction:[ccanimate actionwithanimation:a]];
////////// here zombies:history output.
# address category event type refct timestamp size responsible caller 0 0x13ea95e0 graphicsmanager malloc 1 00:02.748.439 208 -[rowmanager init] 1 0x13ea95e0 graphicsmanager retain 2 00:02.748.447 0 -[graphicsmanager initwithboattype:withword:usingcharacter:orientation:visible:] 2 0x13ea95e0 graphicsmanager release 1 00:02.748.449 0 -[graphicsmanager initwithboattype:withword:usingcharacter:orientation:visible:] 3 0x13ea95e0 graphicsmanager retain 2 00:02.841.543 0 -[ccscheduler scheduleselector:fortarget:interval:paused:repeat:delay:] 4 0x13ea95e0 graphicsmanager retain 3 00:02.841.572 0 ccarrayappendobjectwithresize 5 0x13ea95e0 graphicsmanager retain 4 00:03.477.669 0 -[rowmanager row1:] <--- aha!!!! 6 0x13ea95e0 graphicsmanager retain 5 00:03.482.438 0 -[rowmanager row1finished] 7 0x13ea95e0 graphicsmanager release 4 00:03.482.439 0 -[rowmanager row1finished] 8 0x13ea95e0 graphicsmanager release 3 00:03.482.480 0 -[ccscheduler removehashelement:] 9 0x13ea95e0 graphicsmanager release 2 00:03.483.435 0 ccarrayremoveobjectatindex 10 0x13ea95e0 graphicsmanager release 1 00:03.483.436 0 -[rowmanager row1finished]
you can use (__bridge id)
exclude arc, need exclude & use manual retain-release.
Comments
Post a Comment