The image is an instance of UIImage. The first line executes with no problems, but the second one gives an EXC_BAD_ACCESS error at runtime.
NSLog(#"SCALE: %f", image.scale);
NSLog(#"TEST: %#", NSStringFromCGSize(image.size));
I can view the values of size property in Xcode by mouse-hovering it though.
Can you please help me in understanding what's wrong with it and/or what I might be missing?
Tested on a device and in simulator running iOS 8.
UPD: This is how I'm creating the image:
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGFloat scale = 1.0f;
CGImageRef imageRef = [rep fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:imageRef scale:scale orientation:(UIImageOrientation)rep.orientation];
CGImageRelease(imageRef);
I just tried to delete the last line CGImageRelease(imageRef); and it seem to be fixed the problem. But I do need to release the CGImageRef, since I'm loading very large photos inside a loop and that takes a lot of memory.
So, I figured it out. The problem was that I released a CGImageRef that I didn't own.
CGImageRef imageRef = [rep fullResolutionImage];
If you get a CGImageRef by calling fullResolutionImage, you don't own it. Therefore, you don't need to release it yourself neither.
CGImageRelease(imageRef);
Removing the last line fixed the problem for me.
Related
I have this code:
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *outputimage = [UIImage imageWithCGImage:imageRef
scale:image.scale
orientation:UIImageOrientationUp];
and I get the warning:
Object leaked: object allocated and stored into 'imageRef' is not
referenced later in this execution path and has a retain count of +1
But I am using ARC and cannot use release or autoRelease. How to resolve this?
Just add this code
CGImageRelease(imageRef);
From CGImageCreateWithImageInRect document,
The resulting image retains a reference to the original image, which means you may release the original image after calling this function.
So,what you need to do is just call CGImageRelease to make it retain count -1
I am trying to implement a method that changes the tone of an image in accordance with the slider value, but when i change the slider value continuously it is showing memory warning and the app crashes.
This is my sample code, I tried using dispatch_async
-(void)valueChanged
{
float slideValue = slider.value;
NSLog(#"%0.f",slideValue);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0.5), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[filter setValue:#(slideValue)
forKey:#"inputIntensity"];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage
fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
imageView.image = newImage;
});
});
}
You are leaking memory, what means that you are allocating objects that you are not releasing, hence the memory warnings.
Have you tried releasing the images after you have used them?
CGImageRelease(cgimg)
At the end of the method
Check the documentation for createCGImage:fromRect: API, for return value it says,
You are responsible for releasing the returned image when you no longer need it.
Which means you are leaking memory for cgimg instance. As suggested by Antonio MG you should release the reference at the end. The imageWithCGImage: will retain it, so no need to worry.
Hope that helps!
What are the rules for managing memory for CGImageRefs with ARC? That is, can someone help me to the right documentation?
I am getting images from the photo library and creating a UIImage to display:
CGImageRef newImage = [assetRep fullResolutionImage];
...
UIImage *cloudImage = [UIImage imageWithCGImage:newImage scale:scale orientation:orientation];
Do I need to do CGImageRelease(newImage)?
I'm getting memory warnings but it doesn't seem to be a gradual buildup of objects I haven't released and I'm not seeing any leaks with Instruments. Puzzled I am.
No, you do not need to call CGImageRelease() on the CGImageRef returned by ALAssetRepresentation's convenience methods like fullResolutionImage or fullScreenImage. Unfortunately, at the current time, the documentation and header files for these methods does not make that clear.
If you create a CGImageRef yourself by using one of the CGImageCreate*() functions, then you own it and are responsible for releasing that image ref using CGImageRelease(). In contrast, the CGImageRefs returned by fullResolutionImage and fullScreenImage appear to be "autoreleased" in the sense that you do not own the image ref returned by those methods. For example, say you try something like this in your code:
CGImageRef newImage = [assetRep fullResolutionImage];
...
UIImage *cloudImage = [UIImage imageWithCGImage:newImage
scale:scale orientation:orientation];
CGImageRelease(newImage);
If you run the static analyzer, it will issue the following warning for the CGImageRelease(newImage); line:
Incorrect decrement of the reference count of an object that is not
owned at this point by the caller
Note that you will get this warning regardless of whether your project is set to use Manual Reference Counting or ARC.
In contrast, the documentation for the CGImage method of NSBitmapImageRep, for example, makes the fact that the CGImageRef returned is autoreleased more clear:
CGImage
Returns a Core Graphics image object from the receiver’s
current bitmap data.
- (CGImageRef)CGImage
Return Value
Returns an autoreleased CGImageRef opaque type based on the receiver’s
current bitmap data.
I am trying a simple test for a much more complex project but I am baffled as to why the code below is crashing and giving an EXC_BAD_ACCESS error?
This is called from a UIView.
- (void)testing {
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"ball.png" ofType:nil];
CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
// CGImageRetain(imageRef);
UIImage *newImage = [[UIImage alloc]initWithCGImage:imageRef];
UIImageView *iv = [[UIImageView alloc]initWithImage:newImage];
[self addSubview:iv];
}
My guess is that the CGImageRef is not being retained but adding CGImageRetain(imageRef); makes no difference.
I should also note that this project has ARC turned on.
EDIT
I did a little bit more testing and have discovered that this is directly related to ARC as I created 2 basic projects including only the code above. The first with ARC turned off and it worked perfectly. The next with ARC turned on and BAM crash with the same error. The interesting thing is that I got an actual log error ONLY the first time I ran the project before the crash.
Error: ImageIO: ImageProviderCopyImageBlockSetCallback 'ImageProviderCopyImageBlockSetCallback' header is not a CFDictionary...
This line is the problem:
CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
The created UIImage will be released immediately following this full-expression (e.g. after this line). So even trying to add a CGImageRetain() afterwards won't work.
The fundamental problem is the CGImageRef returned from CGImage is almost certainly an ivar of the UIImage and will be released when the UIImage is deallocted.
The generic way to fix this is to extend the lifetime of the UIImage. You can do this by placing the UIImage into a local variable and referencing it after your last reference to the CGImage (e.g. with (void)uiimageVar). Alternatively, you can retain the CGImageRef on that same line, as in
CGImageRef imageRef = CGImageRetain([[[UIImage alloc] initWithContentsOfFile:imagePath] CGImage]);
But if you do this, don't forget to release the imageRef when you're done.
You wrote this:
CGImageRef imageRef = [[[UIImage alloc]initWithContentsOfFile:imagePath]CGImage];
That line creates a UIImage, gets its CGImage property, and then releases the UIImage object. Since you're not retaining the CGImage on that line, the only owner of the CGImage is the UIImage. So when the UIImage is released and immediately deallocated, it deallocates the CGImage too.
You need to retain the CGImage before the UIImage is released. Try this:
CGImageRef imageRef = CGImageRetain([[UIImage alloc]initWithContentsOfFile:imagePath].CGImage);
and then at the end of the function:
CGImageRelease(imageRef);
I am using the followinf code for applying image filters. This is working fine on scaled down images.
But when I apply more than 2 filters on full resolution images, the app crashes. A memory warning is received.
When I open the 'allocations' instrument, I see that CFData(store) takes up most of the memory used by the program.
When I apply more than 2 filters on a full resolution image, the 'overall bytes' go upto 54MB. While the 'live bytes' don't seem to reach more than 12MB when I use my eyes on the numbers as such, but the spikes show that live bytes also reach upto this number and come back.
Where am i going wrong?
- (UIImage *)editImage:(UIImage *)imageToBeEdited tintValue:(float)tint
{
CIImage *image = [[CIImage alloc] initWithImage:imageToBeEdited];
NSLog(#"in edit Image:\ncheck image: %#\ncheck value:%f", image, tint);
[tintFilter setValue:image forKey:kCIInputImageKey];
[tintFilter setValue:[NSNumber numberWithFloat:tint] forKey:#"inputAngle"];
CIImage *outputImage = [tintFilter outputImage];
NSLog(#"check output image: %#", outputImage);
return [self completeEditingUsingOutputImage:outputImage];
}
- (UIImage *)completeEditingUsingOutputImage:(CIImage *)outputImage
{
CGImageRef cgimg = [context createCGImage:outputImage fromRect:outputImage.extent];
NSLog(#"check cgimg: %#", cgimg);
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
NSLog(#"check newImge: %#", newImage);
CGImageRelease(cgimg);
return newImage;
}
Edit:
I also tried making cgimg as nil. Didn't help.
I tried putting context declaration and definition inside the 2nd function. Didn't help.
I tried to move declarations and definitions of filters inside the functions, didn't help.
AlsoCrash happens at
CGImageRef cgimg = [context createCGImage:outputImage fromRect:outputImage.extent];
the cgimg i was making took most of the space in memory and was not getting released.
I observed that calling the filer with smaller values takes the CFData (store) memory value back to a samller value, thus avoiding the crash.
So I apply filter and after that call the same filter with image as 'nil'. This takes memory back to 484kb or something from 48 MB after applying all 4 filters.
Also, I am applying this filters on a background thread instead of the main thread. Applying on main thread again causes crash. Probably it doesn't get enough time to release the memory. I don't know.
But these things are working smoothly now.
// where is your input filter name like this:
[tintFilter setValue:image forKey:#"CIHueAdjust"];
// I think you have a mistake in outputImage.extent. You just write this
CGImageRef cgimg = [context createCGImage:outputImage fromRect:outputImage extent];