title: Cocos2d replaceScene not releasing the old scene
body: <p>I have a box2d-cocos2d game that has several levels. My levels are layers that use Box2d features inherited from another class. For example the first layer looks like:</p>
  
  <pre><code>Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject
  </code></pre>
  
  <p>My problem is that the layer is not releasing when it should.For example if I replay the Level with this code:</p>
  
  <pre><code>[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5f scene:[[self class] node]]];
  </code></pre>
  
  <p>Then the memory footprint just increases.The same thing happens also if I have a circle like menu -> Level 1 -> menu -> Level 1 and my app just crashes after a time because the insufficient memory.I tracked the dealloc methods with NSLogs, but every dealloc method is called, when I replace the scene.
  My Log looks like:</p>
  
  <pre><code>*** DEALLOC ***&lt;Level1Layer = 0x1cdcfe70 | Tag = -1&gt;
  2013-08-26 12:05:57.089 Myapp[6334:907] 
  ***BOX2DLEVEL DEALLOC ***&lt;Level1Layer = 0x1cdcfe70 | Tag = -1&gt;
  2013-08-26 12:05:57.093 Myapp[6334:907] ***BOX2DLAYER DEALLOC ***
  &lt;Level1Layer = 0x1cdcfe70 | Tag = -1&gt; 
  </code></pre>
  
  <p>I'm really stuck, because on iPhone 4 after playing 3 levels the app crashes.
  How can I solve this problem? Any solutions and pointers are appreciated. </p>
  
  <p>Level 1 dealloc:</p>
  
  <pre><code>-(void)dealloc{
      NSLog(@"\n*** DEALLOC ***%@",self);
      [self removeAllChildrenWithCleanup:YES];
      [super dealloc];
  }
  </code></pre>
  
  <p>Box2dLevel dealloc:</p>
  
  <pre><code>-(void) dealloc{
      NSLog(@"\n***BOX2DLEVEL DEALLOC ***%@",self);
      [self removeChild:_label cleanup:YES];
      [self removeChild:labelForCoins cleanup:YES];
      [self removeChild:labelForTime cleanup:YES];
      [self removeChild:freezeCountDown cleanup:YES];
      [self removeChild:freezedMask cleanup:YES];
      [self removeChild:_backGround cleanup:YES];
      [self removeChild:darkLayer cleanup:YES];
      [self removeChild:flashlayer cleanup:YES];
      [self removeChild:skillsMenu cleanup:YES];
      [arrayForObjects release];
      [skillsMenuArray release];
      [super dealloc];
  }
  </code></pre>
  
  <p>Box2dLayer dealloc:</p>
  
  <pre><code>-(void) dealloc
  {
      NSLog(@"***BOX2DLAYER DEALLOC ***\n%@",self);
      if (debugDraw) {
          delete debugDraw;
          debugDraw = NULL;
      }
      if (mouseJoint) {
          world-&gt;DestroyJoint(mouseJoint);
          mouseJoint = NULL;
      }
      if (groundBody) {
          world-&gt;DestroyBody(groundBody);
          groundBody = NULL;
      }
      if (referenceBody) {
          world-&gt;DestroyBody(referenceBody);
          referenceBody = NULL;
      }
      if (bombSensor) {
          world-&gt;DestroyBody(bombSensor);
          bombSensor = NULL;
      }
      if (laserSensor) {
          world-&gt;DestroyBody(laserSensor);
          laserSensor = NULL;
      }
      if (_contactListener) {
          free(_contactListener);
          _contactListener = NULL;
      }
      [self removeChild:_obj cleanup:YES];
      [super dealloc];
      NSLog(@"\n***Box2dLayer superclass deallocated");
  </code></pre>