Forum Xamarin Xamarin.iOS

Problems with Xamarin.iOS


I just updated to Xamarin.iOS today and ran into a major problem.

I have a custom UIView that looks something like this:
public class CustomView : UIView { public override void Draw(RectangleF rect) { using (var context = UIGraphics.GetCurrentContext()) { using (var cgColor = this.BackgroundColor.CGColor) { context.SetFillColor(cgColor); context.FillRect(this.Bounds); } // ... } } }

Somewhere else in my code, I instantiate the view, set it's background color, and add it to a parent view:
var view = new CustomView(frame); view.BackgroundColor = UIColor.Black; this.Add(view);

The problem is my app occasionally crashes in CustomView.Draw because the BackgroundColor is null. This code worked fine before and is now not working in the latest version of Xamarin.iOS. Any ideas as to why this would occur? The problem is intermittent and only seems to occur with a release build on a real device. If I can't find a solution soon, is it possible to temporarily downgrade to the previous version? I need to finalize testing and publish soon.



  • DimitrisTavlikosDimitrisTavlikos GRInsider, University ✭✭

    I would consider what happens now as correct behavior and if it was working before, incorrect behavior. You are basically disposing your view's background UIColor's CGColor so it's only normal that after the first time Draw is called, BackgroundColor will return null.

    No need to enclose it in a using block. A UIColor's CGColor is not created every time you read it, it's created once and basically the object behind UIColor.

    If, on the other hand, for some reason, changing this code is not easy, I believe you can download the latest major update from your profile page on

    A question, if I may: is there really a need to fill the context with the background color of the view? It seems redundant. The background color of the view is basically the filled rect of the graphics context.

  • MatthewOConnorMatthewOConnor USBeta ✭✭

    Thanks for the quick response. Unfortunately, the BackgroundColor is null the first time Draw() is called, and hence removing the using statement didn't solve the problem.

    I'm also not sure what you mean by the not needing to fill the context with the background color. If I don't call context.SetFillColor(), context.FillRect(), then the background is not what it should be.

  • SebastienPouliotSebastienPouliot CAXamarin Team Xamurai

    We'll need more information to diagnose this. Can you please file a bug report including a self-contained test case that reproduce the issue ?

    Be sure to include which version of iOS (simulator / devices) were used and the previous (working) version of XI you were using.

  • MatthewOConnorMatthewOConnor USBeta ✭✭

    I'm having some problems creating a reproducible test case. In fact, it's difficult to reproduce in my application. I will file a bug report if I can pin it down.

    Based on the Xamarin.iOS release notes, it's looks like something changed in relation to memory management and garbage collection. So it's either a subtle bug in my app that just surfaced or a bug in the Xamarin runtime.

  • SebastienPouliotSebastienPouliot CAXamarin Team Xamurai

    The changes made to the GC (sgen) and NewRefcount are optional so you need to enable them to get their benefits (exiting application won't be using them).

    IOW if you did not change anything else (like build options) then those new features should not affect you.

Sign In or Register to comment.