Hello all,
I'm currently working on an app where I have to record what the user is doing on screen. I do this by using a AVAssetWriterInputPixelBufferAdaptor and writing to it using a CVPixelBuffer which I get from the adaptor's CVPixelBufferPool.
The code works fine, but only for a short time, then I get memory warnings, and the app crashes shortly afterwards. On an iPad 2, this means it crashes roughly 14 seconds after I've started recording the video.
Funnily enough when running on a simulator, there's no apparent memory leakage, it "just" uses roughly 100-140mb extra ram, which in my opinion is 100 more than I'd like.
I found out that the part that makes it all crash is the CVPixelBuffer. So by recommendation by Apple themselves I started using the CVPixelBufferPool from the AVAssetWriterInputPixelBufferAdaptor. But to no avail, I still get the same memory issue, and I cannot see where or why it happens.
Here's the relevant parts of the code which causes the issue:
`private CVPixelBuffer mPxBuffer; private AVAssetWriterInputPixelBufferAdaptor mAvAdaptor;
private void SaveCurrentFrameAtTime(UIImage image, CMTime frameTime) { try { if(image != null) { PixelBufferFromCGImage(image.CGImage);
if (mAvAdaptor != null && mAvAdaptor.AssetWriterInput.ReadyForMoreMediaData && mPxBuffer != null)
{
bool result = mAvAdaptor.AppendPixelBufferWithPresentationTime (mPxBuffer, frameTime);
}
CGImageRelease(image.CGImage.Handle);
}
}
catch(Exception ex)
{
Console.WriteLine ("{0} - {1}", ex.Message, ex.StackTrace);
}
}
private CVPixelBuffer PixelBufferFromCGImage(CGImage image) { CVReturn err = new CVReturn ();
if (mPxBuffer == null)
{
mPxBuffer = mAvAdaptor.PixelBufferPool.CreatePixelBuffer ();// (mCVPixelBufferPoolAllocationSettings, out err);
}
if(mPxBuffer != null)
{
mPxBuffer.Lock (0);
IntPtr pxdata = mPxBuffer.BaseAddress;
if(pxdata != IntPtr.Zero)
{
using(var rgbColorSpace = CGColorSpace.CreateDeviceRGB())
{
using(CGBitmapContext context = new CGBitmapContext(pxdata, image.Width, image.Height, 8, 4 * image.Width, rgbColorSpace, CGImageAlphaInfo.NoneSkipFirst))
{
if(context != null)
{
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
}
context.Dispose ();
}
}
}
mPxBuffer.Unlock(0);
}
return mPxBuffer;
} `
Anybody have any idea what might be the issue here? And more importantly how to solve it?