1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-11 17:39:38 +00:00

mac: fix dangling pointers

initialising UnsafeMutableRawPointer the way we did won't free those
pointers and we get dangling pointers. explicitly define a scope those
pointers are alive and auto freed.
This commit is contained in:
der richter 2020-12-20 20:05:18 +01:00
parent ce1571ac01
commit dae9ea3fa7
2 changed files with 32 additions and 31 deletions

View File

@ -86,17 +86,23 @@ class MPVHelper {
func setOption(fullscreen: Bool) { func setOption(fullscreen: Bool) {
optsPtr.pointee.fullscreen = fullscreen optsPtr.pointee.fullscreen = fullscreen
m_config_cache_write_opt(optsCachePtr, UnsafeMutableRawPointer(&optsPtr.pointee.fullscreen)) _ = withUnsafeMutableBytes(of: &optsPtr.pointee.fullscreen) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(optsCachePtr, ptr.baseAddress)
}
} }
func setOption(minimized: Bool) { func setOption(minimized: Bool) {
optsPtr.pointee.window_minimized = Int32(minimized) optsPtr.pointee.window_minimized = Int32(minimized)
m_config_cache_write_opt(optsCachePtr, UnsafeMutableRawPointer(&optsPtr.pointee.window_minimized)) _ = withUnsafeMutableBytes(of: &optsPtr.pointee.window_minimized) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(optsCachePtr, ptr.baseAddress)
}
} }
func setOption(maximized: Bool) { func setOption(maximized: Bool) {
optsPtr.pointee.window_maximized = Int32(maximized) optsPtr.pointee.window_maximized = Int32(maximized)
m_config_cache_write_opt(optsCachePtr, UnsafeMutableRawPointer(&optsPtr.pointee.window_maximized)) _ = withUnsafeMutableBytes(of: &optsPtr.pointee.window_maximized) { (ptr: UnsafeMutableRawBufferPointer) in
m_config_cache_write_opt(optsCachePtr, ptr.baseAddress)
}
} }
func setMacOptionCallback(_ callback: swift_wakeup_cb_fn, context object: AnyObject) { func setMacOptionCallback(_ callback: swift_wakeup_cb_fn, context object: AnyObject) {
@ -141,4 +147,10 @@ class MPVHelper {
closure(pointers) closure(pointers)
} }
class func getPointer<T>(_ value: inout T) -> UnsafeMutableRawPointer? {
return withUnsafeMutableBytes(of: &value) { (ptr: UnsafeMutableRawBufferPointer) in
ptr.baseAddress
}
}
} }

View File

@ -531,42 +531,36 @@ class Common: NSObject {
events.pointee |= Int32(checkEvents()) events.pointee |= Int32(checkEvents())
return VO_TRUE return VO_TRUE
case VOCTRL_VO_OPTS_CHANGED: case VOCTRL_VO_OPTS_CHANGED:
var o: UnsafeMutableRawPointer? var opt: UnsafeMutableRawPointer?
while mpv.nextChangedOption(property: &o) { while mpv.nextChangedOption(property: &opt) {
guard let opt = o else { switch opt {
log.sendError("No changed options was retrieved") case MPVHelper.getPointer(&mpv.optsPtr.pointee.border):
return VO_TRUE
}
if opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.border) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.window?.border = Bool(mpv.opts.border) self.window?.border = Bool(mpv.opts.border)
} }
} case MPVHelper.getPointer(&mpv.optsPtr.pointee.fullscreen):
if opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.fullscreen) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.window?.toggleFullScreen(nil) self.window?.toggleFullScreen(nil)
} }
} case MPVHelper.getPointer(&mpv.optsPtr.pointee.ontop): fallthrough
if opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.ontop) || case MPVHelper.getPointer(&mpv.optsPtr.pointee.ontop_level):
opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.ontop_level) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.window?.setOnTop(Bool(mpv.opts.ontop), Int(mpv.opts.ontop_level)) self.window?.setOnTop(Bool(mpv.opts.ontop), Int(mpv.opts.ontop_level))
} }
} case MPVHelper.getPointer(&mpv.optsPtr.pointee.keepaspect_window):
if opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.keepaspect_window) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.window?.keepAspect = Bool(mpv.opts.keepaspect_window) self.window?.keepAspect = Bool(mpv.opts.keepaspect_window)
} }
} case MPVHelper.getPointer(&mpv.optsPtr.pointee.window_minimized):
if opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.window_minimized) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.window?.setMinimized(Bool(mpv.opts.window_minimized)) self.window?.setMinimized(Bool(mpv.opts.window_minimized))
} }
} case MPVHelper.getPointer(&mpv.optsPtr.pointee.window_maximized):
if opt == UnsafeMutableRawPointer(&mpv.optsPtr.pointee.window_maximized) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.window?.setMaximized(Bool(mpv.opts.window_maximized)) self.window?.setMaximized(Bool(mpv.opts.window_maximized))
} }
default:
break
} }
} }
return VO_TRUE return VO_TRUE
@ -676,19 +670,14 @@ class Common: NSObject {
return return
} }
var o: UnsafeMutableRawPointer? var opt: UnsafeMutableRawPointer?
while mpv.nextChangedMacOption(property: &o) { while mpv.nextChangedMacOption(property: &opt) {
guard let opt = o else {
log.sendWarning("Could not retrieve changed mac option")
return
}
switch opt { switch opt {
case UnsafeMutableRawPointer(&mpv.macOptsPtr.pointee.macos_title_bar_appearance): case MPVHelper.getPointer(&mpv.macOptsPtr.pointee.macos_title_bar_appearance):
titleBar?.set(appearance: Int(mpv.macOpts.macos_title_bar_appearance)) titleBar?.set(appearance: Int(mpv.macOpts.macos_title_bar_appearance))
case UnsafeMutableRawPointer(&mpv.macOptsPtr.pointee.macos_title_bar_material): case MPVHelper.getPointer(&mpv.macOptsPtr.pointee.macos_title_bar_material):
titleBar?.set(material: Int(mpv.macOpts.macos_title_bar_material)) titleBar?.set(material: Int(mpv.macOpts.macos_title_bar_material))
case UnsafeMutableRawPointer(&mpv.macOptsPtr.pointee.macos_title_bar_color): case MPVHelper.getPointer(&mpv.macOptsPtr.pointee.macos_title_bar_color):
titleBar?.set(color: mpv.macOpts.macos_title_bar_color) titleBar?.set(color: mpv.macOpts.macos_title_bar_color)
default: default:
break break