title: iOS Cocos2d CCLabelTTF Set String Ends in Exc Bad Access
body: <p>This has cost me many hours during the development of an iPhone game. Any help is greatly appreciated!</p>
  
  <h1>Setup</h1>
  
  <ul>
  <li>ARC</li>
  <li>Cocos2d 2.0</li>
  </ul>
  
  <h1>Problem</h1>
  
  <ul>
  <li>EXC_BAD_ACCESS when I set the string on a CCLabelTTF</li>
  <li>Happens roughly 1 out of every 5 tries</li>
  </ul>
  
  <h1>Code</h1>
  
  <p>The following code is from the class LevelSelectionLayer. This class is the delegate to another class that handles a grid of levels and touch interaction.</p>
  
  <h3>In LevelSelectionLayer.h</h3>
  
  <pre><code>@property (strong) NSArray *difficulties;
  @property (strong) CCLabelTTF *difficultyLabel;
  @property (strong) SlidingMenuGrid *slidingMenuGrid;
  </code></pre>
  
  <h3>In LevelSelectionLayer.m</h3>
  
  <pre><code>-(id) init
  {
      if (self = [super init]) {
          self.difficulties = [[NSArray alloc] initWithObjects:@"Easy", @"Intermediate", @"Hard", nil];
  
          [self initDifficultyLabel];
          [self initGrid];
      }
      return self;
  }
  
  -(void) initDifficultyLabel
  {
      CGSize size = [[CCDirector sharedDirector] winSize];
      self.difficultyLabel = [[CCLabelTTF alloc] initWithString:[self.difficulties objectAtIndex:0] fontName:FONT_BOLD fontSize:18.0f];
      self.difficultyLabel.position = ccp(size.width / 2 + 3, size.height / 2 + 63);
      self.difficultyLabel.color = CCC3_DARK_COLOR;
      [self addChild:self.difficultyLabel z:5];
  }
  </code></pre>
  
  <h3>In LevelSelectionLayer.m (continued). This is a delegate method that is called from the grid class mentioned above.</h3>
  
  <pre><code>-(void) slidingGridDidChangePage
  {
      // Get curent page
      int currentPage = self.slidingMenuGrid.iCurrentPage;
      CCLOG(@"currentPage:%d", currentPage); // Always prints correct number
  
      // Adjust pagination display
      for (int i = 0; i &lt; NUM_PAGES; i++) {
          CCSprite *bub = [self.pageBubbles objectAtIndex:i];
          if (i == currentPage) {
              bub.displayFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"pageSelected.png"];
          }
          else {
              bub.displayFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"page.png"];            
          }
      }
  
      // Adjust difficulty labeL
      /******* BREAKS HERE *******/
      self.difficultyLabel.string = (NSString *)[self.difficulties objectAtIndex:currentPage];
      //self.difficultyLabel.string = @"Test"; // Breaks every once in a while as well
  }
  </code></pre>