views:

145

answers:

1

This is my 'tick' function:

- (void) tick: (ccTime) dt
{

 NSLog(@"%d",ticker);
 if(fbut.Adown == YES && ticker > 4)//fbut is a button
 {


  elayer = [[effectsLayer alloc] init]; // each effectlayer draws a //projectile that moves forward 'x' ticks

   elayer.e_playpos = glayer.playerpos; // player position
  [self addChild:elayer z:2];

  [mutable addObject: elayer];
 [elayer release];

  if(mutable.count > 20) // when there are 20 projectiles drawn, start //destroying the last one. 
  {
  NSLog(@"mutable: %d", mutable.count);

   [mutable removeLastObject];
  }  

  ticker=0;
 }
 ticker++;


// .  .  .

this is what the running program looks like

http://screencast.com/t/LpNHL2kJIVpu

looks like more than 20..

interesting thing though, is that the array is holding steady at 20 objects. so if the objects are being 'removed' (via [mutable removeLastObject];) how come they show up on the screen?

Here's the next pickle...

Now i change init to retain( look for the *'s )

- (void) tick: (ccTime) dt
{

 NSLog(@"%d",ticker);
 if(fbut.Adown == YES && ticker > 4)//fbut is a button
 {


  elayer = [[effectsLayer alloc] retain]; // *********each effectlayer draws //a projectile that moves forward 'x' ticks

   elayer.e_playpos = glayer.playerpos; // player position
  [self addChild:elayer z:2];

  [mutable addObject: elayer];
 [elayer release];

  if(mutable.count > 20) // when there are 20 projectiles drawn, start //destroying the last one. 
  {
  NSLog(@"mutable: %d", mutable.count);

   [mutable removeLastObject];
  }  

  ticker=0;
 }
 ticker++;


// .  .  .

And now no effect layers are being drawn, but still the NSArray holds 21 - 20 objects. all of these objects are uninitialized. so i added init to the end: elayer = [[[effectsLayer alloc] retain] init];

now i have the same effects from before.

so i try autorelease.. same effect, lots and lots of pew pew pew's, way more than 20.

my goal is to only have 20 alowed to be drawn at a time and once the 20 are drawn they are deallocated. right now, with out deallocation, the program runs ok untill about 4 mins in when there are about 2000 e layers and the fps is about 5..

why won't the ship pew pew right?

(BTW i m using cocos2d frameworks) this is a project that is copyrighted by me, alex earley 2009.

+7  A: 

First, [[effectsLayer alloc] retain] is terrible. Never do that. Ever. Never use an allocated object that has not been initialized. Furthermore, this will retain the object at least twice, since a call to +alloc returns a retained object, and you're retaining it again, then adding it to an array (which retains it a third time), but it's only getting released twice (when it's removed from the array and your singular release).

I suspect that the problem is with this line: [self addChild:elayer z:2]; What does this method do? Is it in charge of actually drawing the elayer onto the screen? If so, then that means its probably also retaining elayer, which means it's not getting deallocated, because you don't appear to be doing any sort of "removeChild" call when popping things out of your mutable array.

In short: just because something's no longer in your array doesn't mean it's also not on the screen.

Dave DeLong
And even if `addChild:z:` doesn't retain the object but does keep a reference somehow, that just means you still have the layer added as a child, but it's going to crash someday because it's been `dealloc`ed.
Chuck
@Chuck +1 very true.
Dave DeLong
In addition, if you're not retaining it in 'addChild:' you won't limit to 20 items, you'll just likely crash.
bobDevil
oops, didn't refresh the comments. Look at chuck, not me
bobDevil