Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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.
In our application, I am seeing that UIKit.UIImage.AsPNG() returns null for the same CGImage instance after a while. Reading the documentation it seems like the internal data of the CGImage is corrupted/restted somehow:
"This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format."
The CGImage instance in the managed world seems correct, I can retrieve all the properties such as widht, height, colorspace and the refcount returned by CFGetRetainCount of the unmanaged instance returns >0.
Trying to isolate the problem, I came up with a test case  that can reproduce the issue, except that this time it ends up with a native stacktrace attached to bug. I have tried to replicate as much as possible our application setup but in our app there is not segfault, UIKit.UIImage.AsPNG return null instead. The bug is triggered when calling the method Serialize in CGImageBug.Image and it can happen at any time.
The same test case works fine on Android.
Created attachment 13223 [details]
Awesome! We are just starting with Xamarin in mobile platforms and this is the first bug we have to deal with.
Since it's a pretty critical bug to us because it's affecting our core and it's triggered in many places in our application, I'd like to know if there is a workaround for it, like manually retaining a missing reference somewhere.
macccore seems to be a private project so I can't know how it was fixed and if there is an easy workaround :)
The problem is that when you do this (in iOS/Image.cs):
CGDataProvider dataProvider = new CGDataProvider (ser, 0, ser.Length);
CGImage img = CGImage.FromPNG (dataProvider, null, false, CGColorRenderingIntent.Default);
the native CGImage will keep a reference to the native dataProvider object. At some time later, the GC will free the managed dataProvider instance, and we'll (incorrectly) free the 'ser' array with the image data as well, and if the CGImage later flushes the image data and wants to re-read it, it'll read freed memory.
A possible workaround is to make sure the GC doesn't free the dataProvider instance until the CGImage is freed as well: https://gist.github.com/rolfbjarne/ea5787a972e2d50c896d (this will not work if it's possible the managed CGImage can be freed by the GC while the corresponding native object is still alive and expected to render images, since then you'd run into the same problem again).
Another solution (which works all the time, but is slower) is to write the image data to a file, and use CGDataProvider.FromFile to load it again.
That's more or less what I was suspecting, thanks for the confirmation and for the workaround!
Build Info: monotouch-188.8.131.52
I have checked this issue with fixed build provided in this trello card. I observed that this issue does not exist with this build. Now I observed that the attached test sample run successfully without any crash.
This issue has been fixed in master, I will Re-Verify this issue when the fix will merge in release branch.
Build Info: monotouch-184.108.40.206_7b4b6742999ac924e13fec48af48a9d58c5060d8
I have checked this issue with above build and observed that this issue does not exist with this build. Now I observed that the attached test sample run successfully without any crash.
This issue has been fixed I release branch too, hence I am closing this issue.
Environment Info: https://gist.github.com/Rajneesh360Logica/15c8d3fa342d152d527b
*** Bug 37713 has been marked as a duplicate of this bug. ***