Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curious about passRetained in event tap callback #25

Closed
godbout opened this issue Dec 18, 2020 · 4 comments
Closed

curious about passRetained in event tap callback #25

godbout opened this issue Dec 18, 2020 · 4 comments

Comments

@godbout
Copy link

godbout commented Dec 18, 2020

func handle(proxy: CGEventTapProxy, cgEvent: CGEvent) -> Unmanaged<CGEvent>? {

quite noob to macOS development here. been through some coding and loads of documentation in the past few weeks, and i don't get why passRetained is used in your event tap callback rather than passUnretained. i'm pretty sure you're correct, but i can't reason why. from the doc (https://developer.apple.com/documentation/coregraphics/cgeventtapcallback#) it appears the event is owned by the caller. if you modify it, wouldn't you pass it without retaining it? if you use passRetained, then when do you release it?

@creasty
Copy link
Owner

creasty commented Dec 20, 2020

I hate to break it to you but I'm no expert either. This has been the first and only macOS app I've ever created so far.
Perhaps, the simplest reason as to why passRetained being used here is that I found an implementation that used passRetained and I didn't give it much thought...

For what it’s worth, I have never had a memory issue — Consumption-wise, it only uses 15MB at a startup and a week later.
So I assume it might not be critical if we use passRetained or somehow it releases memory at some point. 🤷‍♂️

Anyway, now that you've mentioned it, I've tested both versions and examined the memory usage.
The below are the result of Instruments where I waited 10 sec after the startup, pressed as many random keys as possible for 30 sec, and waited another 20 sec to stop.

passUnretained

Screen Shot 2020-12-20 at 17 34 46

passRetained

Screen Shot 2020-12-20 at 17 36 40

Screen Shot 2020-12-20 at 17 39 24

You're right, there were actually memory leaks.
Wonder how it hasn't been a problem for me...

@godbout
Copy link
Author

godbout commented Dec 20, 2020

ah. thanks for taking the time. and sorry, i meant no macOS/iOS/Swift/Objective-C development here.

i've heard about Instruments but haven't open it yet. seems another big chunk of time and exploration needed to understand that one correctly too. but i'm confused—again—now with your screenshots. i would have expected the leaks to appear on the passRetained one rather than the passUnretained? actually, if passRetained doesn't show any leak, i would have expected passUnretained to simply crash. so, am i misunderstanding how Instruments is reporting leaks?

btw. thanks for making this repo public. inspiring, beautiful code.

@creasty
Copy link
Owner

creasty commented Dec 20, 2020

but i'm confused—again—now with your screenshots. i would have expected the leaks to appear on the passRetained one rather than the passUnretained?

Oh, shoot. It was the other way around. Swapped the titles.

btw. thanks for making this repo public. inspiring, beautiful code.

My pleasure.

@godbout
Copy link
Author

godbout commented Dec 20, 2020

i can die peacefully in my sleep now \o/

thanks again for your time.

@godbout godbout closed this as completed Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants