Question

I am fairly experienced C programmer but IOS is new for me. I do have memory leak (I think this because I get MemoryWarning and soon my app gets killed with no mercy). But Instruments memory leak detector says all is fine. Yes, I have memory snapshotting running every X seconds. Meanwhile if I run VM Tracker I see memory usage is going up. Most of memory is taken by "CG Image" which seems to be clue. I used "if 0"-s to narrow down to place where leak occurs, so this is it:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { 

    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
#if 1
    /****** Draw stuff on every frame screen *******/
    CGImageRef imageRef = image.CGImage;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, image.size.width, image.size.height, 8, image.size.width * 4, colorSpaceRef,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);    
    CGContextDrawImage(context, CGRectMake(0, image.size.height*-0.1, image.size.width, image.size.height*1.2 ),imageRef);
#endif

#if 0
    if (backgroundJobRunning1 == NO) {
        backgroundJobRunning1 = YES;

        NSValue *value1 = [NSValue valueWithPointer:image];
        // start1 = [NSDate timeIntervalSinceReferenceDate];
        NSArray *array = [NSArray arrayWithObjects:value1, nil];
//        [self performSelectorInBackground:@selector(cleverStuffWithImage:) withObject:array ];
        [self cleverStuffWithImage:(array)];
    }
#endif
    if(sum>0) {
        CGPoint p1= CGPointMake(image.size.height-mycorners[0].y ,image.size.width-mycorners[0].x );
        CGPoint p2= CGPointMake(image.size.height-mycorners[1].y ,image.size.width-mycorners[1].x );
        CGPoint p3= CGPointMake(image.size.height-mycorners[2].y ,image.size.width-mycorners[2].x );
        CGPoint p4= CGPointMake(image.size.height-mycorners[3].y ,image.size.width-mycorners[3].x );
        [_layer setpoints:p1 withArg2:p2 withArg3:p3 withArg4:p4];
    }

    /***** Release everything we allocated */
#if 0
    CGImageRef cgImage = CGBitmapContextCreateImage( context );
 //   imageRef = (CGImageRef)[[(id)imageRef retain] autorelease];

    UIImage *image2 = [[UIImage alloc] initWithCGImage:cgImage];
    if(_settingImage == NO) {
        _settingImage = YES;
        [self performSelectorInBackground:@selector(setImageToView:) withObject:image2];
    } else {
        CCLOG(@"missed frame");
    }

    CGImageRelease(cgImage);
#endif

#if 1
    CGColorSpaceRelease( colorSpaceRef );
    CGContextRelease( context );
#endif
}
#if 0
- ( IplImage * )createGrayIplImage: ( UIImage * )image {
    CGImageRef imageRef  = image.CGImage;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    IplImage * iplImage  = cvCreateImage( cvSize( image.size.width, image.size.height ), IPL_DEPTH_8U, 4 );
    CGContextRef context = CGBitmapContextCreate(iplImage->imageData,
                                          iplImage->width,
                                          iplImage->height,
                                          iplImage->depth,
                                          iplImage->widthStep,
                                          colorSpaceRef,
                                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault
                                          );

    CGContextDrawImage( context, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
    CGContextRelease( context );
    CGColorSpaceRelease( colorSpaceRef );

    IplImage * returnImage = cvCreateImage( cvGetSize( iplImage ), IPL_DEPTH_8U, 1 );
    cvCvtColor( iplImage, returnImage, CV_RGBA2GRAY );
    cvReleaseImage( &iplImage );
    return returnImage;
}
#endif
- (void)dealloc {
    [[CCDirector sharedDirector] end];
    [window release];
    [super dealloc];
}

Leaks happens when "#if 1"-s are like now. What I am not releasing?

Actually I had more of similar problems and I assume there is something I do not know about UIImage and CGImage relationship. For example I do not clearly understand if "CGImageRef imageRef = image.CGImage;" allocates something and when it gets released. Trying to release it caused double free error. Meanwhile I pass sometime one of these to background thread and got memory leaks.

Was it helpful?

Solution

Hmm, seems like adding

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

to beginning and

[pool release]

made problem go away. I didn't knew it handles this like thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top