I have a myCGEventCallback function for CGEventTap that takes parameter "refcon:UnsafeMutableRawPointer?".
I pass my main ViewController as a pointer to the callback using
let pointer = UnsafeMutableRawPointer(Unmanaged.passRetained(self).toOpaque())
Then inside the callback, I access the ViewController using
let sender:ViewController = Unmanaged<T>.fromOpaque(refcon!).takeRetainedValue()
When the event occurs, the callback works fine. However, it only works 4 times. When the same event occurs for the fifth time, my app crashes, and the debug console just says "LLDB".
It looks like it crashes when I try to access the sender. "sender.someFunction()". It crashes before the function gets to run, so I assume it has a problem accessing the sender.
Is this because of poor memory management? Maybe I need to deallocate the pointer? If so, how and where would I do that?
Thanks!