ios - Show Camera streaming from frames by using UIImageView memory leak crash


Keywords:ios 


Question: 

I am using QCAR library, and I get the camera Frame from the library in each frame.

I am trying to show this frame by using setImage on my UIImageView* mCurFrameView. This works at first, and I am able to see the frames run smooth, but after 20 seconds it crashes.

Sometimes I get EXC_BAD_ACCESS on

int retVal = UIApplicationMain(argc, argv, nil, nil); 

Sometimes it's just gdb and paused.

Sometimes before he crash I get

2012-02-24 15:59:15.726 QRAR_nextGen[226:707] Received memory warning.

Here's my code:

-(void)SaveCurrentFrame:(UIImage*)image
{

        mCurFrameView.image = image;

}


- (void)renderFrameQCAR
{

   cout<<"I am starting"<<endl;


QCAR::State state = QCAR::Renderer::getInstance().begin();


QCAR::setFrameFormat(QCAR::RGB888, true);

const QCAR::Image *image = state.getFrame().getImage(1);// 0: YUV, 1: Grayscale image

if (image)
{
    const char *data = (const char *)image->getPixels();
    int width = image->getWidth(); int height = image->getHeight();

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapInfo = kCGBitmapByteOrderDefault;
    provider = CGDataProviderCreateWithData(NULL, data, width*height*3, NULL);
    intent = kCGRenderingIntentDefault;
    imageRef = CGImageCreate(width, height, 8, 8*3, width * 3, colorSpace, bitmapInfo,    provider, NULL, NO, intent);

    mCurFrame = [UIImage imageWithCGImage:imageRef];


    cout<<"I am waiting"<<endl;
   [self performSelectorOnMainThread:@selector(SaveCurrentFrame:) withObject:mCurFrame waitUntilDone:YES];

}

I've tried several things: showing CALayer to show the camera, release, retain, autorelease, defining and not defining property and synthesizing.

I'd appreciate it a lot if some one could help me. I am losing my mind. Thanks A lot.


3 Answers: 

You are not releasing (leaking) each CGDataProvider, that means you are storing all the images data on memory. Try doing CGDataProviderRelease(provider) after performSelectorOnMainThread.

 

Maybe you are missing this:

   QCAR::Renderer::getInstance().end();

Here's a snippet working on iPhone 4S/5 iOS 6

- (void)renderFrameIntoImage {
   QCAR::State state = QCAR::Renderer::getInstance().begin();
   QCAR::setFrameFormat(QCAR::GRAYSCALE, true);
   const QCAR::Image *image = state.getFrame().getImage(0); // 0: YUV, 1: Grayscale image
   const char *data = (const char *)image->getPixels();
   int width = image->getWidth(); int height = image->getHeight();
   CGColorSpace *colorSpace = CGColorSpaceCreateDeviceGray();
   CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
   CGDataProvider *provider = CGDataProviderCreateWithData(NULL, data, width*height, NULL);
   CGColorRenderingIntent intent = kCGRenderingIntentDefault;
   CGImageRef imageRef = CGImageCreate(width, height, 8, 8, width * 1, colorSpace, bitmapInfo, provider, NULL, NO, intent);
   UIImage *myImage = [UIImage imageWithCGImage:imageRef];
   QCAR::Renderer::getInstance().end();
   UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, NULL);
}

Regards...

 

You have three leaks.

You must call a Release method for every CoreFoundation method with the word Create in it.

  1. CGColorSpaceRelease(colorSpace);
  2. CGDataProviderRelease(provider);
  3. CGImageRelease(imageRef);

A C++ method to do the QCAR::Image to UIImage conversion

inline UIImage *imageWithQCARCameraImage(const QCAR::Image *cameraImage)
{
    UIImage *image = nil;

    if (cameraImage) {
        CGColorSpaceRef colorSpace = NULL;
        QCAR::PIXEL_FORMAT pixelFormat = cameraImage->getFormat();

        int bitsPerPixel = QCAR::getBitsPerPixel(pixelFormat);

        switch (pixelFormat) {
            case QCAR::RGB888:
                colorSpace = CGColorSpaceCreateDeviceRGB();
                break;
            case QCAR::GRAYSCALE:
                colorSpace = CGColorSpaceCreateDeviceGray();
                break;
            case QCAR::YUV:
            case QCAR::RGB565:
            case QCAR::RGBA8888:
            case QCAR::INDEXED:
                std::cerr << "Image format conversion not implemented." << std::endl;
                break;
            case QCAR::UNKNOWN_FORMAT:
                std::cerr << "Image format unknown." << std::endl;
                break;
        }

        float width = cameraImage->getWidth();
        float height = cameraImage->getHeight();
        int bytesPerRow = cameraImage->getStride();
        const void *baseAddress = cameraImage->getPixels();
        size_t totalBytes = QCAR::getBufferSize(width, height, pixelFormat);

        if (bitsPerPixel > 0 && colorSpace != NULL) {
            CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
                                                                      baseAddress,
                                                                      totalBytes,
                                                                      NULL);

            int bitsPerComponent = 8;
            CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
            CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
            CGImageRef imageRef = CGImageCreate(width,
                                                height,
                                                bitsPerComponent,
                                                bitsPerPixel,
                                                bytesPerRow,
                                                colorSpace,
                                                bitmapInfo,
                                                provider,
                                                NULL,
                                                NO,
                                                renderingIntent);

            image = [UIImage imageWithCGImage:imageRef];
            CGColorSpaceRelease(colorSpace);
            CGDataProviderRelease(provider);
            CGImageRelease(imageRef);
        }
    }

    return image;
}