title: Instruments alleges Memory leak with [UIColor CGColor]
body: <p>I am just in the process of tracking down some memory leaks with Instruments. It claims I am leaking in the middle of a drawRect method. Here is the code:</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect {
      if (highColor &amp;&amp; lowColor) {
          // Set the colors for the gradient to the two colors specified for high and low
  
          // The next line is allegedly leaking
          [gradientLayer setColors:[NSArray arrayWithObjects:(id)[highColor CGColor], (id)[lowColor CGColor], nil]]; 
          gradientLayer.startPoint = CGPointMake(0.5, 0.2);
      }
  
      [super drawRect:rect];
  }
  </code></pre>
  
  <p>I am on the iPad, so I have to manage the memory myself (that is, no garbage collection). Can anyone see what's wrong here? My understanding is that I don't have to release the array nor should I have to release the CGColors. Also, is there any way in Instruments to find out what object type is leaking, ie. is it referring to the NSArray or the CGColors?</p>
  
  <p>Any help would be much appreciated. Thank you.</p>
  
  <p>PS: I got the code for the GradientView from somewhere some months ago; it works very well (other than exposing the aforementioned memory leak). You can find the code <a href="http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/" rel="nofollow" title="here">here</a>.</p>
  
  <p>EDIT:</p>
  
  <p>I have done a bit more research and refactored my code as follows:</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect {
      if (highColor &amp;&amp; lowColor) {
          // The following two lines are leaking
          CGColorRef highCGColor = [highColor CGColor];
          CGColorRef lowCGColor = [lowColor CGColor];
  
          // Set the colors for the gradient to the two colors specified for high and low
          [gradientLayer setColors:[NSArray arrayWithObjects:(id)highCGColor, (id)lowCGColor, nil]];
          gradientLayer.startPoint = CGPointMake(0.5, 0.2);
  
          CGColorRelease(highCGColor);
          CGColorRelease(lowCGColor);
      }
  
      [super drawRect:rect];
  }
  </code></pre>
  
  <p>However, I can't work out why the two CGColors are still leaking. I am releasing them at the end of the method. Is it possible that the NSArray does not release them properly when it is deallocated? Still puzzled...</p>