Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appendPixelBuffer EXC_BAD_ACCESS #4

Open
berio opened this issue Jul 15, 2014 · 41 comments
Open

appendPixelBuffer EXC_BAD_ACCESS #4

berio opened this issue Jul 15, 2014 · 41 comments

Comments

@berio
Copy link

berio commented Jul 15, 2014

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!

@tobira
Copy link

tobira commented Aug 19, 2014

i getting the same error,any solution now?

@alskipp
Copy link
Owner

alskipp commented Aug 19, 2014

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

@alskipp
Copy link
Owner

alskipp commented Aug 19, 2014

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

@tobira
Copy link

tobira commented Aug 20, 2014

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.

@tobira
Copy link

tobira commented Aug 20, 2014

By the way,will you adding sounds?

@alskipp
Copy link
Owner

alskipp commented Aug 24, 2014

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.

@tobira
Copy link

tobira commented Aug 25, 2014

Thanks for your suggestions,that is helpful!

@nihtin9mk
Copy link

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];

@alskipp
Copy link
Owner

alskipp commented Sep 16, 2014

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.

@nihtin9mk
Copy link

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.

@alskipp
Copy link
Owner

alskipp commented Sep 18, 2014

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.

@nihtin9mk
Copy link

So kind of you. Thank you @alskipp.

I am also trying to find out the issue.

@alskipp
Copy link
Owner

alskipp commented Sep 18, 2014

Just tried the record, quit, record routine several times with my own app - I've not yet been able to reproduce the crash 😕

@nihtin9mk
Copy link

That is bit strange - Please check my attached image
screen shot 2014-09-18 at 7 28 22 pm

@berio
Copy link
Author

berio commented Sep 18, 2014

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.

@berio
Copy link
Author

berio commented Sep 19, 2014

I just updated xcode and ipad to ios8 and the EXC_BAD_ACCESS error disappeared. Everything works now like a charm!

@alskipp
Copy link
Owner

alskipp commented Sep 19, 2014

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.

@nihtin9mk
Copy link

Hi @alskipp - waiting for your solution.

@PerMartin
Copy link

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.

@alskipp
Copy link
Owner

alskipp commented Sep 23, 2014

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

@nihtin9mk
Copy link

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.

@nihtin9mk
Copy link

Hi @alskipp .. Waiting for your response.

@alskipp
Copy link
Owner

alskipp commented Oct 3, 2014

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

@nihtin9mk
Copy link

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];

@kellyts
Copy link

kellyts commented Nov 17, 2014

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?

@ranjk89
Copy link

ranjk89 commented Nov 18, 2014

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.

@manderson-productions
Copy link

@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

@manderson-productions
Copy link

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

@Rovemoon
Copy link

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.

@Rovemoon
Copy link

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);
            }

@andrewzeus
Copy link

I tested it out and still have the same problem. Does anyone have a solution for that? thanks a lot in advance.

@mineshpurohit
Copy link

Hello All,
I am also getting the same issue. is any one able to solve that issue then please share resolution.
Thanks in advance.

screen shot 2015-12-29 at 6 15 18 pm

@jgorecki-zz
Copy link

I'm getting sporadically on different devices. iPhone 5s with 8.4 it happens everytime, but on an iPhone 6 running 9.1 it never happens. I'm getting sporadic crash reports from a device tester with an iPhone 6+ running 9.2.

@azubala
Copy link

azubala commented Jan 26, 2016

I'm experiencing the same issue, in my case steps to reproduce is to start recording in landscape mode and then quickly change orientation to portrait.

Bad Access exception has the following back trace:

* thread #6: tid = 0x74a32, 0x39bd61dc libsystem_platform.dylib`_platform_memmove$VARIANT$CortexA9 + 160, queue = 'ASScreenRecorder.append_queue', stop reason = EXC_BAD_ACCESS (code=1, address=0x2bfb000)
    frame #0: 0x39bd61dc libsystem_platform.dylib`_platform_memmove$VARIANT$CortexA9 + 160
    frame #1: 0x286607ac CoreMedia`FigNEAtomWriterAppendData + 52
    frame #2: 0x28660664 CoreMedia`sbufAtom_appendAtomWithMemoryBlock + 64
    frame #3: 0x2865f0aa CoreMedia`sbufAtom_createSerializedDataForPixelBuffer + 438
    frame #4: 0x2865edea CoreMedia`FigRemote_CreateSerializedAtomDataForPixelBuffer + 142
    frame #5: 0x29b54ac4 MediaToolbox`remoteWriter_AddPixelBuffer + 88
    frame #6: 0x265ee1f4 AVFoundation`-[AVFigAssetWriterTrack addPixelBuffer:atPresentationTime:error:] + 104
    frame #7: 0x265ea79c AVFoundation`-[AVAssetWriterInputWritingHelper appendPixelBuffer:withPresentationTime:] + 120
    frame #8: 0x265e854a AVFoundation`-[AVAssetWriterInput _appendPixelBuffer:withPresentationTime:] + 82
    frame #9: 0x265ed38a AVFoundation`-[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] + 90

EDIT
I performed number of measurements using Instruments: Allocations, Leaks and Zombies and there's no clear indication what is the problem. However I have a strong feeling that this crash is happening due to the temporal excessive memory usage - in my case it's easily to reproduce on older devices, e.g. iPod.

My temporary workaround is to scale down the pixel buffer size on low-end devices.

@JeffHyde
Copy link

JeffHyde commented Feb 4, 2016

I hade the same issue and @Rovemoon has the good solution, the crashing occurs significantly less often. Inside The writeVideoFrame method do this.
screen shot 2016-02-04 at 3 51 12 pm
.

@jabson
Copy link

jabson commented Apr 13, 2016

@weitiedan
Copy link

I have the same issue, it happens at the same point from time to time. @alskipp ,have you found the final solution? please tell we,thank you, I had a week time for this crash.

@msobiepanek-uz
Copy link

My answer to this issue -> http://stackoverflow.com/a/38044065/5608231
I don't see this error anymore.

@MrChrisBarker
Copy link

The stack overflow post (http://stackoverflow.com/questions/34086827/on-screen-recording-video-crashing-in-ios-8/38044065#38044065) seems to solve the issue for me too, but it now wont save the recording..... any ideas?

@xjwang1126
Copy link

AVAssetWriterInputPixelBufferAdaptor is not thread safe, so keep AVAssetWriterInputPixelBufferAdaptor not in multi threads.

@ameinc
Copy link

ameinc commented Apr 6, 2017

I have this issue too. My workaround was trying to retain the buffer:

CVPixelBufferRef pixelBuffer = NULL;
CVPixelBufferRetain(pixelBuffer); // hope to retain the buffer
CGContextRef bitmapContext = [self createPixelBufferAndBitmapContext:&pixelBuffer];

It seems to work without crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests