Bug 5486 - ReplaceSubView in a layer-backed view hierarchy calls NSView.set_Layer with null CALayer
Summary: ReplaceSubView in a layer-backed view hierarchy calls NSView.set_Layer with n...
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: unspecified
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Miguel de Icaza [MSFT]
Depends on:
Reported: 2012-06-03 15:13 UTC by steven.orth
Modified: 2012-08-01 15:30 UTC (History)
2 users (show)

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

Project that reproduces crash on set_Layer w/ null value. (19.78 KB, application/zip)
2012-07-31 17:23 UTC, steven.orth

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 steven.orth 2012-06-03 15:13:16 UTC
In this situation, our code has a NSWindow.ContentView which has set WantsLayer to true. At a later point, a subview is replaced with a different (newly created) subview. During the call to NSView.ReplaceSubview(), our override of set_Layer crashes with ArgumentNullException (which seems to be by design):

MonoMac.AppKit.NSView.set_Layer (Parameters=) in /cvs/monomac/src/AppKit/NSView.g.cs:2253
NationalInstruments.Core.Controls.NSViewBase.set_Layer (Parameters=) in /Users/steveno/projects/NextGen/R2/PlatformFramework/Dev-ToolViewModels/Core/Core/Controls/NSViewBase.MonoMac.cs:57
object.[NationalInstruments.Core.Controls.NSViewBase:Void set_Layer(MonoMac.CoreAnimation.CALayer)] (Parameters=) in 
MonoMac.AppKit.NSView.ReplaceSubviewWith (Parameters=) in /cvs/monomac/src/AppKit/NSView.g.cs:468

Questions / Notes:
1. Have not tried this with MonoMac 3.0 yet, so we're not sure if this behavior has changed.
2. The ArgumentNullException does not occur (or is not visible to our code) when we do not override the Layer property. Presumably, the operation may still be occurring, but is handled internally by MonoMac.AppKit.
3. Is simply not calling base.set_Layer() the appropriate behavior when the system attempts to assign a null CALayer? (None of our code ever explicitly assigns a null CALayer.)
4. Are there ramifications w.r.t. "leaking" any objects?

Thank you.
Comment 1 Miguel de Icaza [MSFT] 2012-06-22 12:15:09 UTC

Can you provide a code snippet that shows what you are doing?
Comment 2 steven.orth 2012-07-31 17:23:44 UTC
Created attachment 2284 [details]
Project that reproduces crash on set_Layer w/ null value.

Build and run the attached project. Click the button in the lower left (Replace a View). It will crash in the set_Layer override on the NSView subclass.
Comment 3 Miguel de Icaza [MSFT] 2012-08-01 15:30:10 UTC
Fixed on git