Window.CancelClose
Declare Sub release Lib "Foundation" Selector "release" (receiver As Ptr)
release(Ptr(Handle))
I can confirm this workaround fixes the problem, tested in RB2017R3, 32 and 64 bit Cocoa apps.
If you are in the habit of subclassing all Xojo classes, then this is a simple 2-line fix for your project. (Instead of using a Window class directly, I define cWindow as a subclass of Window, and then all my project Windows are set to cWindow).
Good point - if your window returns True from CancelClose (preventing .Close from happening) this would not be a good thing. Putting it in .Close may be safer. I just tested it, and it seems to work fine in .Close.
Window.CancelClose
Declare Sub release Lib "Foundation" Selector "release" (receiver As Ptr)
release(Ptr(Handle))
I can confirm this workaround fixes the problem, tested in RB2017R3, 32 and 64 bit Cocoa apps.
If you are in the habit of subclassing all Xojo classes, then this is a simple 2-line fix for your project. (Instead of using a Window class directly, I define cWindow as a subclass of Window, and then all my project Windows are set to cWindow).[/quote]
Yes. A Xojo window on macOS is a native NSWindow. Like Xojo, macOS uses ARC to determine if a class instance has to be kept alive. “Release” lowers the reference count by 1. Once it reaches 0, the memory of the instance is released. This way there might not be a native object behind the Xojo object anymore, which could cause a NilObjectException.
Better check for the Xojo version and increase its value in case this bug will not be fixed in the next release.
Like (if 2018r1 will contain the fix)
#If TargetMacOS And XojoVersion <= 2017.03
Declare Sub release Lib "Foundation" selector "release" (id As Integer)
release Me.handle
#EndIf
Put this code in either a subclass (like Michael) or a module, that way if it needs to be removed it easily done, rather than modifying 20 different windows (or worse forgetting 1 and then customers experience crashes).
I can’t beleive that it’s as simple as Xojo forgetting to release the NSWindow; there has to be a reason as why it’s done this way? Maybe they’re using a NSWindowController or something, there’s some reason as to why the window is not being release; I’d love to know why.
Manually taking control of an objects reference that you personally didn’t init or retain is a recipe for a future disaster, this is the voice of experience here.