内存泄漏。乐器认为一切都很好

时间:2012-06-25 01:36:50

标签: iphone ios memory-leaks uiimage cgimageref

我是一位经验丰富的C程序员,但IOS对我来说很新。我确实有内存泄漏(我认为这是因为我得到了MemoryWarning,很快我的应用程序就会毫不留情地被杀死)。但仪器内存检漏仪说一切都很好。是的,我每隔X秒运行一次内存快照。同时,如果我运行VM Tracker,我发现内存使用率正在上升。大多数记忆都是由“CG Image”拍摄的,这似乎是线索。我使用“if 0”-s缩小到发生泄漏的位置,所以就是这样:

- (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];
}

当“#if 1”-s像现在一样时发生泄漏。我没有发布什么?

实际上我有更多类似的问题,我认为有一些我不了解UIImage和CGImage的关系。例如,我不清楚是否“CGImageRef imageRef = image.CGImage;”分配一些东西,什么时候发布。试图释放它导致双重免费错误。与此同时,我将其中一个传递给后台线程并获得内存泄漏。

1 个答案:

答案 0 :(得分:2)

嗯,好像在添加

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

开头和

[pool release]
问题消失了。我不知道它处理这个线程。