monkid
monkid

Reputation: 69

Get [Not a type retain] and [Not a type class] message sent to deallocated instance

I am developing an app which can watch web cam video. Now I want to add a new record video function by using a background thread.

in .h file

@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSData *imageData;

in .m file. When user press record button, It start saving ImageData to Documents Dir

@synthesize image;
@synthesize imageData
...

- (void) playVideo:(NSData *)data { 
        ...
     // play video code
     CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
     CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     CGImageRef cgImage = CGImageCreate(width, hieght, 8, 24,
                            bpp * width, colorSpace, bitmapInfo,
                            provider, NULL, NO, kCGRenderingIntentDefault); 
    CGColorSpaceRelease(colorSpace);
    [videoImage = [setImage: UIImage imageWithCGImage:cgImage]];

    CGImageRelease(cgImage);
    CGDataProviderRelease(provider);
    // play video code end
    ...
    if(record == TRUE) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
            (unsigned long)NULL), ^(void) {
            image = [UIImage imageWithCGimage:cgImage];
            imageData = [UIImageJPEGRepresentation(image, 1)];
            [imageData writeToFile:DirPath atomically:NO];
        });
    }
}

Play video code seems to work fine so far. Then I add thread below it and got error messages

[Not a type retain]: message sent to deallocated instance 
[Not a type class]: message sent to deallocated instance
in thread
image = [UIImage imageWithCGimage:cgImage];
Can you tell me why cause this? or Have other way to do this. thanks.

Upvotes: 2

Views: 582

Answers (2)

yinkou
yinkou

Reputation: 5766

You are releasing cgImage before you dispatch. -> CGImageRelease(cgImage);
Move this line to the end (inside) of your block.

Upvotes: 1

jere
jere

Reputation: 4304

If cgImage is of CGImageRef type, then that means it's a pointer to a C-struct, try casting it to an Objective-C object:

        //set play video code like this
        [videoImage setImage: [UIImage imageWithCGImage:(id)cgImage]];

Upvotes: 0

Related Questions