swift - CGEventTap, CGEvent & NSEvent Memory Leak -
i'm having tough time releasing cgevent once convert nsevent (which need). i'm doing using cgeventtap. documentation states
returns: autoreleased nsevent object equivalent cgevent
the returned object retains cgeventref object (cgevent) until (the objective-c object) freed—it releases cgeventref object. if no cocoa event corresponds cgeventref object, method returns nil.
so have written code convert nsevent , set nsevent nil (as test). after setting nil, either nsevent or cgevent not released , memory keeps going every mouse move. i'm not entirely sure why. feel i'm missing fundamental here.
here's code.
func eventcallback(proxy: cgeventtapproxy, type: cgeventtype, var event: cgevent, refcon: unsafemutablepointer<void>) -> unmanaged<cgevent>? { //this line causes memory leak if var e: nsevent? = nsevent(cgevent: event) { e = nil //according docs, should decrease reference count event } return unmanaged.passunretained(event!) }
as @willeke commented, autoreleasepool
fixed memory leak me.
swift should automatically cleaning nsevents you, setting e
nil shouldn't necessary inside autoreleasepool.
here's worked me:
func eventcallback(proxy: cgeventtapproxy, type: cgeventtype, var event: cgevent, refcon: unsafemutablepointer<void>) -> unmanaged<cgevent>? { autoreleasepool{ var e: nsevent? = nsevent(cgevent: event) // code using e } return unmanaged.passunretained(event) }
Comments
Post a Comment