title: Problems with NSArray of CGImageRef's (but not on simulator!)
body: <p>I am trying to put a few CGImageRefs into an NSArray. It works as it should on the simulator, but not on the device - it crashes at the point where I try to form the array with a EXC_BAD_ACCESS code = 1 error. I tried other things too, like starting with a mutable array but it doesn't work either. The result of typing bt in the console is</p>
  
  <pre><code>(lldb) bt
  * thread #1: tid = 0x2403, 0x3a9b25b6 libobjc.A.dylib`objc_msgSend + 22, stop reason = EXC_BAD_ACCESS (code=1, address=0x86ea78f7)
  frame #0: 0x3a9b25b6 libobjc.A.dylib`objc_msgSend + 22
  frame #1: 0x32a98b5e CoreFoundation`+[__NSArrayI __new:::] + 58
  frame #2: 0x00042388 NameOfMyApp
  </code></pre>
  
  <p>So basically I have a number of images, of letters, and I have a number of layers representing positions in the word that should each cycle through them. For example, layer1 displays letterC, then letterF, then letterP etc, while layer2 displays some other sequence.</p>
  
  <p>I thought I'd load each letter once...</p>
  
  <pre><code>CGImageRef letterH = [UIImage imageWithContentsOfFile:letterHfilename].CGImage;
  CGImageRef letterI = [UIImage imageWithContentsOfFile:letterIfilename].CGImage;
  </code></pre>
  
  <p>...and put the CGImageRefs in an NSArray property, cast to id:</p>
  
  <pre><code>self.titleLetters = @[(__bridge id)letterH, (__bridge id)letterI;
  </code></pre>
  
  <p>I then later do</p>
  
  <pre><code>layerN.contents = self.titleLetters[i];
  </code></pre>
  
  <p>but more importantly I have to be able to put them (may times each) in ANOTHER array:</p>
  
  <pre><code>[contentsValuesArray addObject:self.titleLetters[j]];
  </code></pre>
  
  <p>and then use that array as the values of a keyFrameAnimation:</p>
  
  <pre><code>CAKeyframeAnimation* contentsAnim = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
  contentsAnim.values = contentsValuesArray;
  </code></pre>
  
  <p>All this works fine in the simulator, every time.</p>
  
  <p>As I said, I tried some variations, and the only thing that seems to work is when I form a mutable array with just one letter-imageref in it, using arrayWithObject. Even then, it only works sometimes, and it crashes further down in my programme.</p>
  
  <p>I seem to have read somewhere that the CGImageRefs are not retained, so that they may be released before I even get to use them. Since I began coding after ARC was introduced I know little about releasing, and since it has always worked on the simulator, I figured that didn't apply in my case. </p>
  
  <p>Also, in this reply:</p>
  
  <p><a href="https://stackoverflow.com/questions/1392321/how-do-i-create-a-mutable-array-of-cgimagerefs">How do I create a mutable array of CGImageRefs?</a></p>
  
  <p>a person working on Cocoa Frameworks says that it should work although he doesn't give any specifics, so it might be implied. </p>
  
  <p>Any help and suggestions would be much appreciated.</p>
  
  <p><strong>EDIT SOLVED</strong>
  It seems that the problem was indeed that the imagerefs did not exist when I tried to add them to the array, and this was because they had been released already. Apparently they need to be retained using CGImageRetain. It now works, I just need to make sure they are released afterwards.</p>