title: Objective-c Memory warning
body: <p>I am trying to make some kind of puzzles, but after 5-10 shuffles I receive memory warning, and now I am trying just to add puzzle and remove it from super view but, however, there is still some kind of memory leek.. could some one help me please?</p>
  
  <pre><code>- (void)setup6x6
  {
      int num = 0;
      for (int i = 0; i &lt; 6; i++)
      {
          for (int j = 0; j &lt; 6; j++)
          {
              UIImageView * imgView = [[UIImageView alloc] init];
  
              cor = arc4random()%4;
              nCor = arc4random()%8;
              maxCor = arc4random()%16;
  
              scale = _pImage.size.width / puzzleArea.frame.size.width;
              NSString * imageName1;
              UIImage * imageToMask1;
              UIImage * maskedImage1;
              if (i == 0)
              {
                  if (j == 0)
                  {
                      imageName1 = [topLeftCornerPuzzles objectAtIndex:cor];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake(0,
                                                                                58 * i * scale,
                                                                                58 * scale,
                                                                                58 * scale)];
                  }
                  else if (j == 5)
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[topRightCornerPuzzles objectAtIndex:cor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake((42 + 50*(j-1)) * scale,
                                                                                58 * i * scale,
                                                                                58 * scale,
                                                                                58 * scale)];
                  }
                  else
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[topPuzzles objectAtIndex:nCor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake((42 + 50*(j-1)) * scale,
                                                                                58 * i * scale,
                                                                                66 * scale,
                                                                                58* scale)];
                  }
              }
              else if (i &lt; 5)
              {
                  if (j == 0)
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[leftPuzzles objectAtIndex:nCor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake(0,
                                                                                (42 + 50*(i-1)) * scale,
                                                                                58 * scale,
                                                                                66 * scale)];
                  }
                  else if (j == 5)
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[rightPuzzles objectAtIndex:nCor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake((42 + 50*(j-1)) * scale,
                                                                                (42 + 50*(i-1)) * scale,
                                                                                58 * scale,
                                                                                66 * scale)];
                  }
                  else
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[middlePuzzles objectAtIndex:maxCor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake((42 + 50*(j-1)) * scale,
                                                                                (42 + 50*(i-1)) * scale,
                                                                                66 * scale,
                                                                                66 * scale)];
                  }
              }
              else
              {
                  if (j == 0)
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[bottomLeftCornerPuzzles objectAtIndex:cor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake(0,
                                                                                (42 + 50*(i-1)) * scale,
                                                                                58 * scale,
                                                                                58 * scale)];
                  }
                  else if (j == 5)
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[bottomRightCornerPuzzles objectAtIndex:cor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake((42 + 50*(j-1)) * scale,
                                                                                (42 + 50*(i-1)) * scale,
                                                                                58 * scale,
                                                                                58 * scale)];
                  }
                  else
                  {
                      imageName1 = [self checkPreviousInArray6x6:puzzles new:[bottomPuzzles objectAtIndex:nCor] index:num];
                      imageToMask1 = [self cropImage:_pImage withRect:CGRectMake((42 + 50*(j-1)) * scale,
                                                                                (42 + 50*(i-1)) * scale,
                                                                                66 * scale,
                                                                                58 * scale)];
                  }
              }
              [puzzles addObject:imageName1];
              maskedImage1 = [self maskImage:imageToMask1 withMask:[UIImage imageNamed:imageName1]];
              [imgView setImage:maskedImage1];
  
              if (i == 0)
                  originY = 0;
              else
                  originY = 50 * (i-1) + 42;
  
              if (j == 0)
                  originX = 0;
              else
                  originX = 50 * (j-1) + 42;
  
              if (j == 0 || j == 5)
                  sizeW = 58;
              else
                  sizeW = 66;
  
              if (i == 0 || i == 5)
                  sizeH = 58;
              else
                  sizeH = 66;
  
              [imgView setFrame:CGRectMake(originX, originY, sizeW, sizeH)];
              [puzzleArea addSubview:imgView];
              [imgView removeFromSuperview];
              //[maskedImage1 release];
              imageToMask1 = nil;
              maskedImage1 = nil;
              [imgView release];
              [imageName1 release];
              //[imageToMask1 release];
              num += 1;
          }
      }
  }
  </code></pre>
  
  <p>There is how I crop:</p>
  
  <pre><code>- (UIImage *)cropImage:(UIImage *)image withRect:(CGRect)cropRect
  {
      return [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage, cropRect)];
  }
  </code></pre>
  
  <p>And there is how I mask:</p>
  
  <pre><code>- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImg
  {
      CGImageRef maskRef = maskImg.CGImage;
      CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                          CGImageGetHeight(maskRef),
                                          CGImageGetBitsPerComponent(maskRef),
                                          CGImageGetBitsPerPixel(maskRef),
                                          CGImageGetBytesPerRow(maskRef),
                                          CGImageGetDataProvider(maskRef),
                                          NULL,
                                          false);
      return [UIImage imageWithCGImage:CGImageCreateWithMask([image CGImage], mask)];
      CGImageRelease(maskRef);
      CGImageRelease(mask);
  }
  </code></pre>
  
  <p>I can't understand why do I receive a memory warning if I release everything. However, if i try to release maskedImage1 or imageToMask1 I also receive error...</p>