Workaround false-positive memory leak detected in objc sample

This commit is contained in:
SvyatoslavScherbina
2020-06-09 10:51:07 +03:00
committed by GitHub
parent 260a0e6631
commit 7cad336013
+9 -1
View File
@@ -157,12 +157,20 @@ class MyAppDelegate() : NSObject(), NSApplicationDelegateProtocol {
backgroundColor = NSColor.grayColor()
releasedWhenClosed = false
delegate = object : NSObject(), NSWindowDelegateProtocol {
val delegateImpl = object : NSObject(), NSWindowDelegateProtocol {
override fun windowShouldClose(sender: NSWindow): Boolean {
NSApplication.sharedApplication().stop(this)
return true
}
}
// Wrapping to autoreleasepool is a workaround for false-positive memory leak detected:
// NSWindow.delegate setter appears to put the delegate to autorelease pool.
// Since this code runs during top-level val initializer, it misses the autoreleasepool in [main],
// so the object gets released too late.
autoreleasepool {
delegate = delegateImpl
}
}
val buttonPress = NSButton(NSMakeRect(10.0, 10.0, 100.0, 40.0)).apply {