Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Keep a local ref of the NSImage so it doesn't try to get reconstructed #1118

Merged
merged 2 commits into from
Mar 13, 2023

Conversation

marshall-lerner
Copy link
Contributor

@marshall-lerner marshall-lerner commented Mar 10, 2023

VSMac is getting some exceptions that we are trying to resurrect the image in this code, so keep a local copy of the NSImage...

Here is the part of the exception callstack:

"ObjCRuntime.Runtime.MissingCtor(IntPtr,IntPtr,Type,MissingCtorResolution)", "ObjCRuntime.Runtime.ConstructNSObject[T](IntPtr,Type,MissingCtorResolution)",
"ObjCRuntime.Runtime.GetNSObject[T](IntPtr)",
"AppKit.NSImageView.get_Image()",
"Xwt.Mac.ImageViewBackend.SetImage(ImageDescription)", "Xwt.ImageView.set_Image(Image)",

VSMac is getting some exceptions that we are trying to resurrect the image in this code, so keep a local copy of the NSImage... 

Here is the part of the exception callstack:

"ObjCRuntime.Runtime.MissingCtor(IntPtr,IntPtr,Type,MissingCtorResolution)",
"ObjCRuntime.Runtime.ConstructNSObject[T](IntPtr,Type,MissingCtorResolution)",
"ObjCRuntime.Runtime.GetNSObject[T](IntPtr)",
"AppKit.NSImageView.get_Image()",
"Xwt.Mac.ImageViewBackend.SetImage(ImageDescription)",
"Xwt.ImageView.set_Image(Image)",
@mrward mrward merged commit 405a722 into mono:main Mar 13, 2023
@marshall-lerner marshall-lerner deleted the patch-1 branch March 13, 2023 14:27
@@ -53,16 +53,20 @@ protected override Size GetNaturalSize ()
NSImage img = Widget.Image;
return img == null ? Size.Zero : img.Size.ToXwtSize ();
}

NSImage nsImage; // Needed to keep a ref, otherwise Xam.Mac might GC it and try to resurrect it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this ViewBackend override Dispose(bool) and dispose the nsImage? I don't really know when IWidgetBackend.Dispose is called, maybe we don't need to care about this? Just a thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! If Widget still holds a reference to the native part of NSImage, and ViewBackend doesn't dispose of Widget when it gets disposed, would disposing of the nsImage put it into a weird state? What if we just nulled out the nsImage?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure. Nulling it out is probably better than nothing in terms of spreading out GC pressure.

BTW, you can now rewrite GetNaturalSize to use nsImage.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants