ASScreenRecorder
ASScreenRecorder copied to clipboard
appendPixelBuffer EXC_BAD_ACCESS
Hi, first of all, thanks for the code, it is really helpful. I am getting a EXC_BAD_ACCESS error in this line:
BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
inside writeVideoFrame method.
Do you have any clue about what would be wrong?
Thank you very much!
i getting the same error,any solution now?
Hi folks, Sorry for the late response to this issue. Do you have any more details to help me track down the issue? Does this occur with the demo app, or with your own use case? Does it crash immediately, or does it record for a while first? I'll investigate the issue, but any further details would be very helpful (extra error info, stack trace etc).
Al
In the - (void)writeVideoFrame
method towards the end there is the following code:
// append pixelBuffer on a async dispatch_queue, the next frame is rendered whilst this one appends
// must not overwhelm the queue with pixelBuffers, therefore:
// check if _append_pixelBuffer_queue is ready
// if it’s not ready, release pixelBuffer and bitmapContext
if (dispatch_semaphore_wait(_pixelAppendSemaphore, DISPATCH_TIME_NOW) == 0) {
dispatch_async(_append_pixelBuffer_queue, ^{
BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
if (!success) {
NSLog(@"Warning: Unable to write buffer to video");
}
CGContextRelease(bitmapContext);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
dispatch_semaphore_signal(_pixelAppendSemaphore);
});
} else {
CGContextRelease(bitmapContext);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
}
Could you try replacing it with the following to see if it solves the issue?
BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
if (!success) {
NSLog(@"Warning: Unable to write buffer to video");
}
CGContextRelease(bitmapContext);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
Current implementation uses a separate queue to append pixel buffers which increases the video frame rate. However, this is quite possibly the cause of the issue. Let me know if this prevents the crash - if so, it will unfortunately also lead to a drop in frame rate. I'll see if there's anything I can do to solve this.
Al
It occur with my own app.Sometimes it work perfect.It crash both immediately and record for a while. the "time" always be null when "BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];" called. I have tried to replacing those code as your suggestion,unfortunately it didn't work.
By the way,will you adding sounds?
Hi @tobira, With regards to sound recording, this is not currently planned due to time constraints. Take a look at my response to #3, I give a few suggestions which might point you in the right direction. Thanks for trying the suggested fix, but sorry it didn't work. The fact the time variable becomes null is pretty suspicious, I'm not sure why this should be the case, I'll see if I can discover why.
Thanks for your suggestions,that is helpful!
Hi @tobira @alskipp ...Could you find out this issue ? I am experiencing the same issue and stuck in my app.
I think this is some memory issue. If i finish recording screen and again hit the record button the app crashes always at the same place - BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
It's kind of promising that it crashes consistently with the same action! The problem I have is that I've been unable to reproduce the crash. I've tested on an iPhone 5s and 4S and I've not managed to cause the EXC_BAD_ACCESS. Needless to say, this makes it very difficult to track down the bug.
@nihtin9mk I suspect that if the crash occurs every time you start a second recording that this is related to creating the first frame of your live video capture. I'll continue this discussion on issue #5.
HI @alskipp - please try to record a screen and after finishes quit the app and again start recording, suddenly it crashes at times. Hopefully you can find the issue and solve this as you are the creator of this awesome control.
An the issue is not related to second video capture, I have described in issue #5.
Hi @nihtin9mk, I'll have a bit more spare time this weekend and I'll do my best to locate the problem. I'll try the record, quit, then record to see if I can reproduce the crash.
So kind of you. Thank you @alskipp.
I am also trying to find out the issue.
Just tried the record, quit, record routine several times with my own app - I've not yet been able to reproduce the crash 😕
That is bit strange - Please check my attached image
Hi, sorry for being out, I was in holidays. Yes, that is exactly the same error that I have. I couldn't find any solution yet. I would be more than glad if it could be fixed. This piece of code is pretty helpful.
I just updated xcode and ipad to ios8 and the EXC_BAD_ACCESS error disappeared. Everything works now like a charm!
I'd like to say I fixed the bug by force of will alone, but that might not be entirely accurate.
I'll have more to say on the matter when I return home. With any luck the mystery bug fix continues to work for you.
Hi @alskipp - waiting for your solution.
Hi I have had the same problem, getting EXC_BAD_ACCESS. I solved by using XCodes Analyze function. It pointed out, that there was a potential memory leak in "createPixelBufferAndBitmapContext" in the line "return bitmapContext;" I changed the code, so that the functionality from createPixelBufferAndBitmapContext was put into the calling function "writeVideoFrame", and now it seems to be working.
Hi @PerMartin, thanks for investigating the issue - that sounds very promising. Why didn't I think of running the static analyser? I've been spending my time trying to get the thing to crash in my app (and failing) - multi-threaded memory bugs are a nightmare!
I'll alter the way createPixelBufferAndBitmapContext is called and see if that fixes the crash for everyone. If it does (and you happen to live in London), then I owe you a pint of finest ale!
Unfortunately I won't have time to make the changes tonight - hopefully tomorrow.
All the best, Al
Hi @alskipp..I know you are so busy, but could you figure out the issue. I am trying to fix it. @PerMartin - Hi man, good to hear that you fixed that - can you post some codes.
Hi @alskipp .. Waiting for your response.
Apologies for the delay, but things have been a bit hectic lately.
I've added an experimental branch to the repository: https://github.com/alskipp/ASScreenRecorder/tree/experimental
Could you check this out and see if it works for you? All I've done is create the CGContext in thewriteVideoFrame
method as suggested by @PerMartin. The static analyser is now happy - let me know how you get on.
Al
Thank you @alskipp , for the help - I have tried your solution, but unfortunately it is getting crashed at the point BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
I have also tried the experimental branch and have the same crash: BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time]; Has there been any progress in resolving this?
I have the same issue, it happens at the same point from time to time. Trying to debug, no luck yet. Any guidance will be appreciated.
Another issue on iPhone 5c when you switch/minimize application when recording the video, recording stops when you come back to the app, [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time] returns NO.
@alskipp This is occurring for me mostly on an iPhone 6 device running iOS 8. The crash occurs almost every time. On an iPhone 5 running iOS 8 it only crashes every now and then, but always on the line mentioned above. I am doing a lot of [UIView animations] and my thoughts were that the use of semaphores was somehow trying to access a pixelBuffer that is somehow already released from the view context (just a random theory...). Trying my best to debug. Here is a Crashlytics report, hopefully somewhat helpful but my guess is no:
4
Crashed: ASScreenRecorder.append_queue
EXC_BAD_ACCESS KERN_PROTECTION_FAILURE at 0x0bb49000
raw
0
libsystem_platform.dylib
_platform_memmove + 485
1
2
CoreMedia
FigNEAtomWriterAppendData + 52
3
CoreMedia
sbufAtom_appendAtomWithMemoryBlock + 64
4
CoreMedia
sbufAtom_createSerializedDataForPixelBuffer + 452
5
CoreMedia
FigRemote_CreateSerializedAtomDataForPixelBuffer + 214
6
MediaToolbox
remoteWriter_AddPixelBuffer + 88
7
AVFoundation
-[AVFigAssetWriterTrack addPixelBuffer:atPresentationTime:error:] + 104
8
AVFoundation
-[AVAssetWriterInputWritingHelper appendPixelBuffer:withPresentationTime:] + 120
9
AVFoundation
-[AVAssetWriterInput _appendPixelBuffer:withPresentationTime:] + 80
10
AVFoundation
-[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] + 90
11
Spring
ASScreenRecorder.m line 379
__35-[ASScreenRecorder writeVideoFrame]_block_invoke309
12 libdispatch.dylib
_dispatch_call_block_and_release + 10
13
libdispatch.dylib
_dispatch_queue_drain + 952
14
libdispatch.dylib
_dispatch_queue_invoke + 84
15
libdispatch.dylib
_dispatch_root_queue_drain + 338
16
libdispatch.dylib
_dispatch_worker_thread3 + 94
17 libsystem_pthread.dylib
_pthread_wqthread + 668
I also got this from a Sentry report (XCode always breaks on the same line but never seems to throw the exception in the console)
NSInvalidArgumentException: ** -[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] invalid parameter not satisfying: pixelBuffer != ((void)0)
This is one of those things that I don't know enough about to say "ah its because x,y, or z is causing the bug". I have used several other libraries that do this sort of thing. One is Glimpse (which basically creates an array of UIImages, then at the end of the recording, processes them). It doesn't use a CVPixelBufferPool but rather draws a CGImage into the CVPixelBuffer and appends that into a video frame.
One thing that I tried was to assign pixel buffer attributes (the same ones that are assigned to your CVPixelBufferPool actually) and this seemed to cause a lot less crashing on my devices. However, I eventually got the crash shown at the top of this comment...any thoughts on whether the issue lies in the attributes not being set for the pixel buffer adapter? i.e. your line:
_avAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:_videoWriterInput sourcePixelBufferAttributes:nil];
Sorry if that sounds completely stupid, but I'm just trying to compare the differences between Glimpse https://github.com/wess/Glimpse (which works flawlessly on iphone 4, 5, 5s, 6, and 6+, both ios 7 and 8) and yours (which seems to crash more frequently on some devices, particularly iphone 6). I would prefer your library because it is wayyy more efficient and processes the output file extremely quickly. I'll keep digging and let you know if I have any revelations on this!
Mark
Thanks very much for all above discussion and it really works out now, special thanks to alskipp for developing nice codes, and publish the solution which works well on my devices now. I also thanks to PerMartin for his solution, this is the right answer.
sorry but it not solve my problem, it happens again, so I add below codes to avoid NULL, seems it works fine now.
if (pixelBuffer != NULL)
{
BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
if (!success) {
NSLog(@"Warning: Unable to write buffer to video");
}
CGContextRelease(bitmapContext);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
dispatch_semaphore_signal(_pixelAppendSemaphore);
}