Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSFontDescriptor.Create crashes #19659

Closed
tipa opened this issue Dec 16, 2023 · 2 comments · Fixed by #19663
Closed

NSFontDescriptor.Create crashes #19659

tipa opened this issue Dec 16, 2023 · 2 comments · Fixed by #19663
Labels
api-bindings bug If an issue is a bug or a pull request a bug fix
Milestone

Comments

@tipa
Copy link

tipa commented Dec 16, 2023

Steps to Reproduce

var fd = NSFont.SystemFontOfSize(10).FontDescriptor;
var serif = fd.Create(NSFontDescriptorSystemDesign.Serif);
var rounded = fd.Create(NSFontDescriptorSystemDesign.Rounded);

Expected Behavior

No crash, maybe return null?

Actual Behavior

Crash (that cannot be caught with try-catch)

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib          0x184f838f0 objc_retain + 8
1   CoreFoundation       0x1853e81cc __NSSingleEntryDictionaryI_new + 204
2   CoreFoundation      0x1853bc170 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 52
3   UIFoundation          0x189f90514 -[NSFontDescriptor fontDescriptorWithDesign:] + 176
4   App                          0x104a1f670 xamarin_dyn_objc_msgSend + 160 (trampolines-arm64-objc_msgSend.inc.s:99)

Environment

.NET8

@rolfbjarne rolfbjarne added bug If an issue is a bug or a pull request a bug fix api-bindings labels Dec 18, 2023
@rolfbjarne
Copy link
Member

This is a bug on our side, we bound this incorrectly. I'll fix it.

@rolfbjarne rolfbjarne added this to the Future milestone Dec 18, 2023
@rolfbjarne
Copy link
Member

Workaround:

Add this to your project, and call CreateWithDesign instead of Create:

namespace AppKit {
	using System;
	using System.Runtime.InteropServices;

	using Foundation;
	using ObjCRuntime;

	public static class NSFontDescriptor_Extensions {
		// Workaround for https://github.com/xamarin/xamarin-macios/issues/19659.
		public static NSFontDescriptor? CreateWithDesign (this NSFontDescriptor fd, NSFontDescriptorSystemDesign design)
		{
			var nsb_design = global::AppKit.NSFontDescriptorSystemDesignExtensions.GetConstant (design);
			var rv = objc_msgSend (fd.GetHandle (), Selector.GetHandle ("fontDescriptorWithDesign:"), nsb_design.GetHandle ());
			GC.KeepAlive (nsb_design);
			return Runtime.GetNSObject<NSFontDescriptor> (rv);
		}

		[DllImport ("/usr/lib/libobjc.dylib")]
		static extern IntPtr objc_msgSend (IntPtr handle, IntPtr selector, IntPtr value);
	}
}

and then do:

var fd = NSFont.SystemFontOfSize(10).FontDescriptor;
var serif = fd.CreateWithDesign(NSFontDescriptorSystemDesign.Serif);
var rounded = fd.CreateWithDesign(NSFontDescriptorSystemDesign.Rounded);

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Dec 18, 2023
[NSFontDescriptor fontDescriptorWithDesign:] takes an NSString, not the enum
value, so use BindAs to bind correctly.

Fixes xamarin#19659.
rolfbjarne added a commit that referenced this issue Jan 2, 2024
[NSFontDescriptor fontDescriptorWithDesign:] takes an NSString, not the enum
value, so use BindAs to bind correctly.

Fixes #19659.
vs-mobiletools-engineering-service2 pushed a commit to vs-mobiletools-engineering-service2/xamarin-macios that referenced this issue Jan 2, 2024
[NSFontDescriptor fontDescriptorWithDesign:] takes an NSString, not the enum
value, so use BindAs to bind correctly.

Fixes xamarin#19659.
vs-mobiletools-engineering-service2 pushed a commit to vs-mobiletools-engineering-service2/xamarin-macios that referenced this issue Jan 3, 2024
[NSFontDescriptor fontDescriptorWithDesign:] takes an NSString, not the enum
value, so use BindAs to bind correctly.

Fixes xamarin#19659.
dalexsoto pushed a commit that referenced this issue Jan 3, 2024
….Create. Fixes #19659. (#19731)

[NSFontDescriptor fontDescriptorWithDesign:] takes an NSString, not the
enum
value, so use BindAs to bind correctly.

Fixes #19659.


Backport of #19663

---------

Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-bindings bug If an issue is a bug or a pull request a bug fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants