in Education by
I have a code that generates new image every time user moves slider a little and adds it to sublayer. It works except it leaks memory. What I am retaining that i shouldn't? However i think i am releasing everything as i should: UISlider *slider = (UISlider *)sender; int progressAsInt = (int)(slider.value); //add moon mask UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), NO, 1); CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.8); CGContextSetRGBStrokeColor(contextRef, 0, 0, 0, 0.8); int tempx = progressAsInt; int x = tempx; if (x < 0) { isHigherThanHalf = YES; x = -tempx; } else { isHigherThanHalf = NO; } CGContextSaveGState(contextRef); BOOL onlyDrawTopHalf = isHigherThanHalf; CGFloat halfMultiplier = onlyDrawTopHalf ? -1.0 : 1.0; CGRect ellipse = CGRectMake(200-x/2, 0, 0+x, 400); CGRect clipRect = CGRectOffset(ellipse, halfMultiplier * ellipse.size.width / 2, 0); CGContextClipToRect(contextRef, clipRect); CGContextFillEllipseInRect(contextRef, ellipse); CGContextRestoreGState(contextRef); CGContextSaveGState(contextRef); onlyDrawTopHalf = !onlyDrawTopHalf; halfMultiplier = onlyDrawTopHalf ? -1.0 : 1.0; ellipse = CGRectMake(0, 0, 400, 400); clipRect = CGRectOffset(ellipse, halfMultiplier * ellipse.size.width / 2, 0); CGContextClipToRect(contextRef, clipRect); CGContextFillEllipseInRect(contextRef, ellipse); CGContextRestoreGState(contextRef); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); //must free pixels free(pixels); pixels = (Byte *)[data bytes]; CGImageRef imageRef = image.CGImage; // create a new image from the modified pixel data size_t width = CGImageGetWidth(imageRef); size_t height = CGImageGetHeight(imageRef); size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef); size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef); size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL); CGImageRef newImageRef = CGImageCreate ( width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault ); // the modified image UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; sublayer2.contents = (id)[newImage CGImage]; [self.view.layer addSublayer:sublayer2]; // cleanup [data release]; CGColorSpaceRelease(colorspace); CGDataProviderRelease(provider); CGImageRelease(newImageRef); I have added [data release] but there still seems to be some leaking. EDIT - I have found that in addition to [data release] i need to close my image context by calling UIGraphicsEndImageContext(); The leaks have stopped. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); data object is not released. you are using autorelease image object sometime it can results in memoryWarning. UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; It is always better to create object manually and then release when you are done with the object. because we never know when the autorelase pool will be drain.

Related questions

0 votes
    I have a code that generates new image every time user moves slider a little and adds it to sublayer ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Which one of the following causes memory leak? (a) Release database connection when querying is complete (b) ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which one of the following causes memory leak? (a) Release database connection when querying is complete (b ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    I will be using Wordpress as a CMS with custom post types. Each "post" is a product and will ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    I focus on a project to add image support based on OmniGroup's rtf editor.I met the problem when ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
0 votes
    How can we fix a leaking flick mixer tap?...
asked Jan 28, 2021 in General by JackTerrance
0 votes
    What is meant by a memory leak in Java, and how to implement it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    What is meant by a memory leak in Java, and how to implement it. Select the correct answer from above ... ,Core Questions, Core Hadoop MCQ,core interview questions for experienced...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    I'm trying to checkout a large directory and part way through, the SVN server crashed. I've tried ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    As a follow up to my previous question, I am wondering how to use transparent windows correctly. If I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    what configuration needs to be tweaked, and where does it live, in order to increase the maximum allowed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    what configuration needs to be tweaked, and where does it live, in order to increase the maximum allowed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...