Bug 16750 - CVPixelBufferPool.CreatePixelBuffer leaks the pixel buffers it produces
Summary: CVPixelBufferPool.CreatePixelBuffer leaks the pixel buffers it produces
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.4.x
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 7.2.1
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-12-11 20:38 UTC by Adam Kemp
Modified: 2014-03-24 07:04 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Example project (48.19 KB, application/zip)
2013-12-11 20:38 UTC, Adam Kemp
screenshot of instrument memory allocations (24.80 KB, image/png)
2014-03-21 10:32 UTC, Sebastien Pouliot

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Adam Kemp 2013-12-11 20:38:57 UTC
Created attachment 5647 [details]
Example project

When you obtain a CVPixelBuffer from a CVPixelBufferPool the obtained buffer is double retained so it gets leaked. Adding an explicit call to CVBufferRelease before calling Dispose fixes the leak.

This is similar to this other bug:

The CreatePixelBuffer method should use the alternative constructor (IntPtr, bool) to indicate ownership of the object.

Steps to reproduce:
1. Build and run the attached iPad app.
2. In the app press the "Record" button in the toolbar. If you run on a real device then the app will likely be killed due to memory usage after a few seconds. In the simulator memory usage just grows to huge levels. Use Instruments or (for the simulator) Activity Monitor to observe memory usage grow rapidly.
Comment 1 narayanp 2013-12-12 01:06:44 UTC
I have checked this issues with following builds:

Mac Maverick
X.S 4.2.2(build 2)

iPad 2 version 6.0.1

I am also able to reproduce this issue. This is the iOS Device log for the same: iOS device log: https://gist.github.com/AkhileshKumar01/650424a5f53d26b4d704
Comment 2 Sebastien Pouliot 2013-12-12 20:11:25 UTC
Took nearly 2 minutes on an iPad 3 but it did crash with a low memory (using master).
Comment 3 Sebastien Pouliot 2013-12-12 21:13:29 UTC
Fixed in master 75a9c2075ab53356b39a34ee9007435179620e87

It's been running for over 8 minutes without a single memory warning.

Thanks for the test case!
Comment 4 narayanp 2014-03-20 09:34:59 UTC
I have checked this attached sample with following builds:

master X.iOS
X.S 4.2.4(build 32)
Mono 3.2.6

Also checked with X.iOS 7.2.1-20

Application still getting crashed after clicking on 'Record' button. This is the iOS device log: https://gist.github.com/saurabh360/05412a06448cb0dc08a4

Also getting exception when clicking on 'Record' button on Simulator. This is exception details: https://gist.github.com/saurabh360/59230f4afcc098f6e3c0

Hence reopening this issue
Comment 5 Sebastien Pouliot 2014-03-20 09:39:21 UTC
It does not look to be the same issue:

Mar 24 14:23:35  VideoTest[4071] <Warning>: Unhandled managed exception: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[UIWindow drawViewHierarchyInRect:afterScreenUpdates:]: unrecognized selector sent to instance 0x21092a70 (MonoTouch.Foundation.MonoTouchException)

Please include which type of device and iOS version was used.

That's even more important if you do not supply the crash report (and a symbolication crash report) should be attached for every crash that happens on devices.
Comment 6 narayanp 2014-03-20 10:08:08 UTC
I am getting this exception on
iPad 3 version 6.1.2
iOS Simulator below version 7.0

Also I am not getting symbolicated log in Xcode but I am getting console log: https://gist.github.com/saurabh360/d44795d5b2aa2f65c6d2 for this exception

However, On iPad 2 New version 7.1 and iOS Simulator 7.0 and 7.1 Application does not crash after clicking on 'Record' button and in Application Output it is showing "2014-04-13 19:17:34.664 VideoTest[828:707] Received memory warning" when It's been running for over 5 minutes on physical device
Comment 7 Sebastien Pouliot 2014-03-21 10:18:42 UTC
The reason for your crash is that the test case requires iOS 7.0+ since it calls:

[Export ("drawViewHierarchyInRect:afterScreenUpdates:"), Since (7, 0), CompilerGenerated]
public virtual bool DrawViewHierarchy (RectangleF rect, bool afterScreenUpdates)

I'll look if I can reproduce the memory warnings…
Comment 8 Sebastien Pouliot 2014-03-21 10:32:11 UTC
Created attachment 6375 [details]
screenshot of instrument memory allocations

I can see the memory warnings - but only on device[1] when not using Instruments.

OTOH but there's no leak, i.e. it's a normal warning - there's a lot of objects being created and if the GC is not called soon enough a memory warning will be issued [3].

[1] because the simulator calls GC.Collect every few seconds

[2] which slows down execution a bit and allow the GC to be called

[3] which XI translate into a CG.Collect, solving the memory issue
Comment 9 Tajinder Singh 2014-03-24 07:04:55 UTC
As per comment 7(this test case requires 7.0+ device to run) and 8 marking this issue as verified fixed.

Environment Details:
XS 4.2.4(build 32)