1

我是相当有经验的 C 程序员,但 IOS 对我来说是新的。我确实有内存泄漏(我认为这是因为我收到 MemoryWarning 并且很快我的应用程序就会被毫不留情地杀死)。但是 Instruments 内存泄漏检测器说一切都很好。是的,我每 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 和 CGI​​mage 关系我不知道一些事情。例如,如果“CGImageRef imageRef = image.CGImage;”我不清楚 分配一些东西以及它何时被释放。试图释放它会导致双重释放错误。同时,我有时将其中一个传递给后台线程并出现内存泄漏。

4

1 回答 1

2

嗯,好像加了

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

开始和

[pool release]

使问题消失。我不知道它像线程一样处理这个。

于 2012-06-25T01:41:57.473 回答