views:

818

answers:

3

Hi, im trying to cut a image and mask it....that im able to do successfully..but the program exits after few minutes with 101 status

- (void) maskImage {

        if(scopeOn==1){

 UIGraphicsBeginImageContext(self.bounds.size);
 [self.layer renderInContext:UIGraphicsGetCurrentContext()];
 cachedImage=[UIImage imageNamed:@"loop.png"];
 cachedImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

  imageRef = [cachedImage CGImage];

  subImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(scopeLoc.x-25, scopeLoc.y-25, 50, 50));
  xMaskedImage = CGImageCreateWithMask(subImage, mask);
 zoomImg.image = [UIImage imageWithCGImage:xMaskedImage]; // retImage;
 [zoomImg setCenter:scopeLoc];

 [self addSubview:zoomImg];

 CGImageRelease(subImage);
            CGImageRelease(xMaskedImage);

}

}

this is the code that im using....since im not allocating explicit memory my guess is that CGImageCreateWithImageInRect function is allocating memory but it is not being released...this function is called after every 0.1 secs...so eventually a large amount of memoey is allocated(i have seen this in memory leak performance monitor)

So is there any other way in which i can achive the same wihtout this function??

+3  A: 

Are you releasing the subImage variable later? CGImageCreateWithImageInRect follows the "Create" rule from CoreFoundation, and thus requires you to release the variable later.

NilObject
As suggested i released the CGImageRef object, no memory leaks occured but it is still consuming memory...it goes upto 40mb and then it exits with status 101...I have update the whole function in the post that i have been using, this function is being called per 0.1sec...please help.....
Snehal
what about xMaskedImage? it may have a similar problem
cobbal
yea did it...but no change it still exists :(
Snehal
Where is "mask" coming from? Is that also being properly released when it's no longer being used? I can't see any other missed releases from this method, and would assume they're somewhere else. If you run instruments you should be able to narrow down where it's coming from.
NilObject
A: 

Maybe try adding a test for if zoomImg is already a subview:

if (zoomImg.superview != self)
    [self addSubview:zoomImg];

although this is a fairly long shot.

cobbal
thanx...but didnt work :(
Snehal
A: 

Hi

You can release the memory allocated for your subImage and xMaskedImage by following


    GImageRelease(subImage);
    subImage=nil;
 CGImageRelease(xMaskedImage);
 xMaskedImage=nil;

It will definitely solved your problem.

Jyotsna