Bug 16505 - linker drops NSPrintPreviewGraphicsContext, causing a crash
Summary: linker drops NSPrintPreviewGraphicsContext, causing a crash
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: mmp ()
Version: 1.6.19
Hardware: PC Mac OS
: Normal normal
Target Milestone: 2.0.x
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-11-28 22:52 UTC by Ademar Gonzalez
Modified: 2015-04-29 14:33 UTC (History)
4 users (show)

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

XS project (5.76 MB, application/zip)
2013-11-28 22:52 UTC, Ademar Gonzalez

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 Ademar Gonzalez 2013-11-28 22:52:34 UTC
Created attachment 5563 [details]
XS project

When enabling the linker the class NSPrintPreviewGraphicsContext is not linked in.

As a result when the user access the Print dialog via File -> Print or Cmd + P the application crashes with the following exception.

Unhandled Exception:
System.ArgumentException: Could not find a valid superclass for type NSPrintPreviewGraphicsContext. Did you forget to register the bindings at MonoMac.ObjCRuntime.Class.Register() or call NSApplication.Init()?

To reproduce:

- Run the attached project (linking is enabled)
- Go to File -> Print
Comment 1 Sebastien Pouliot 2013-12-02 11:14:47 UTC
`NSPrintPreviewGraphicsContext` is a special type and we'll add the code to handle it in a future release. In the meantime you should be able to workaround this by adding:

    typeof (NSPrintPreviewGraphicsContext);

somewhere inside your code. The linker will see (and mark) the type and it will be available at runtime. That will allow you to re-enable the linker.
Comment 2 Sebastien Pouliot 2015-03-12 17:43:03 UTC
Fixed in maccore/master d047ee123f3bfc28126bb9258878fccfed8d1c7a
Backported in maccio/macios-cycle5 31b1c12e4d8890b585da9e8016b10246f3d0c6f1

The workaround (from comment #1) won't be needed in XM 2.0+
Comment 3 Rajneesh Kumar 2015-03-13 14:56:19 UTC
I have checked this issue with the following builds:

Xamarin Studio Version 5.9 (build 210)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Mono 4.0.0 ((detached/1bd3b8a)
GTK+ 2.24.23 (Raleigh theme)
Package version: 400000051
Apple Developer Tools Xcode 6.2 (6776)
Xamarin.Mac Version: (Business Edition)
=== Build Information ===
Release ID: 509000210
Git revision: f32ddb8c1f9836ba5e018ac51056008e9a1ac208
Build date: 2015-03-12 10:06:56-04
Xamarin addins: abfe34dd3ad6ef1a3b720d6cd74441c8e7aa22ff
Operating System Mac OS X 10.9.5
Darwin MacMini.local 13.4.0 Darwin Kernel Version 13.4.0
Sun Aug 17 19:50:11 PDT 2014
root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64

I observed that this issue is working fine. Now, user when access the Print dialog via File -> Print or Cmd + P the application does't crash. 
Here is the screencast for the same: http://www.screencast.com/t/c6qX4iS3

This issue has been fixed, hence I am closing this issue.
Comment 4 Radu 2015-04-29 14:33:47 UTC
I am doing a custom scheme pagination and also custom page border drawing. It seems that getting NSGraphicsContext.CurrentContext in DrawPageBorder (of the view being printed) leads to the following unhandled error:
Unhandled Exception:

MonoMac.RuntimeException: The ObjectiveC class 'NSPrintPreviewGraphicsContext' could not be registered, it does not seem to derive from any known ObjectiveC class (including NSObject).
  at MonoMac.Registrar.DynamicRegistrar.Lookup (IntPtr class, Boolean throw_on_error) [0x00000] in <filename unknown>:0 
  at MonoMac.ObjCRuntime.Class.Lookup (IntPtr klass, Boolean throw_on_error) [0x00000] in <filename unknown>:0 
  at MonoMac.ObjCRuntime.Class.Lookup (IntPtr klass) [0x00000] in <filename unknown>:0 
  at MonoMac.ObjCRuntime.Runtime.GetNSObject[NSGraphicsContext] (IntPtr ptr) [0x00000] in <filename unknown>:0 
  at MonoMac.AppKit.NSGraphicsContext.get_CurrentContext () [0x00000] in <filename unknown>:0 
  at ProjectViewer_mac.SimpleReportRenderer.DrawPageBorder (SizeF borderSize) [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) MonoMac.ObjCRuntime.Messaging:bool_objc_msgSend (intptr,intptr)
  at MonoMac.AppKit.NSPrintOperation.RunOperation () [0x00000] in <filename unknown>:0 
  at ProjectViewer_mac.MyDocument.PrintReport (ReportsStandardEnum reportType) [0x00000] in <filename unknown>:0 
etc ...

In debug config this does not happen. Fortunately, I got away with the workaround suggested by Sebastien Pouliot. This issue should be reviewed and reopened or a new issue should be filed.