0

My first post is at here to look for the reason why my app is crashing. After almost 3 hours to detect and now I can find out where the leak is.

First of all, what I am doing is after downloading an image using AFNetworking, I will be setting the size of uiimageview regarding to the size of downloaded image.

[self.headerView setImageWithURLRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:fundraiserInfo.imageUrl]]
                       placeholderImage:nil
                                success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                    // Use following to set the size of image according to retina or non retina
                                    CGImageRef sourceImageRef   =   [image CGImage];
                                    UIImage *newImage           =   [UIImage imageWithCGImage:sourceImageRef scale:[image scale]                                                                                           orientation:[image imageOrientation]];
                                    CGImageRelease(sourceImageRef); // <------ this is I am getting crash
                                    weakObj.image  = newImage;                                                                                                    
                                 }
                                 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                        ;
                                 }
 ];

I am getting warning like below

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

How I can detect the leak :

Putting that line and run the app, the app is crashing after several tries. By detecting after 2 hours, I can reproduce the error. If I remove that line and follow exactly what I do to reproduce the error, the app is running properly and no crashing at all.

Any thought why that line is the reason to crash my app

Community
  • 1
  • 1
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
  • Why you are CGImageRelease(sourceImageRef); releasing this unless you have not created or retain it? – βhargavḯ Mar 02 '13 at 06:50
  • @Bhargavi:did I create it by using `[image CGImage]`, didn't I ? That is why I release it after `newImage` is created. I may be wrong about this. – tranvutuan Mar 02 '13 at 07:02

1 Answers1

3

You are only referencing sourceImageRef using CGImageRef sourceImageRef = [image CGImage]; . More over you are not creating, retaining it, so it is not your responsibility to release it.

I would like you to look at post Releasing CGImageRef.

Also look at Link 1 and Link 2

Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59