user1752054
user1752054

Reputation: 382

AVAssetsWriter CVBufferRelease(buffer); causing a crash

AVAssetsWriter CVBufferRelease(buffer); causing a "Thread 1:EXC_BAD_ACCESS (code=1, address = 0x1ac05beb8) crash.

I'm working on an app that combines images into a video. It works perfectly well unless somehow the appending processes is interrupted by the user closing the app or the app crashing.The next time the app is launched, this crash occurs and can only be resolved by deleting and reinstalling the app. Below is the code I'm using to append the images. When the crash occurs, CVBufferRelease(buffer); is highlighted by Xcode.

Thanks in advance for your help.

CVPixelBufferRef buffer = NULL;

//convert uiimage to CGImage.

int frameCount = 0;

for(UIImage * img in imageArray)
{
        buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:size];

        BOOL append_ok = NO;
        int j = 0;
        while (!append_ok && j < 30) 
        {
            if (adaptor.assetWriterInput.readyForMoreMediaData) 
            {
                printf("appending %d attemp %d\n", frameCount, j);

                CMTime frameTime = CMTimeMake(frameCount,(int32_t) kRecordingFPS);
                append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

                if(buffer)
                    CVBufferRelease(buffer);
                [NSThread sleepForTimeInterval:0.05];
            } 
            else 
            {
                printf("adaptor not ready %d, %d\n", frameCount, j);
                [NSThread sleepForTimeInterval:0.1];
            }
            j++;
        }
        if (!append_ok) {
            printf("error appending image %d times %d\n", frameCount, j);
        }
        frameCount++;
    }
}

Upvotes: 2

Views: 447

Answers (1)

user1752054
user1752054

Reputation: 382

After a lot of back and forth I managed to spot the issue. I overlooked the fact that the path where the video was being appended needs to be cleared at the start of every writing session.

if([[NSFileManager defaultManager] fileExistsAtPath:path]){
    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}

Upvotes: 1

Related Questions