Domanda

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

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top