-
Notifications
You must be signed in to change notification settings - Fork 948
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
Macos multi windows leak #481
Conversation
Just a little reminder, same as #428:
So all
|
@edwin0cheng hi! thank you, fixed! |
Alright, I'm back on macOS, so I can review this now. First of all, those statistics look great! I can still reproduce #158 on this branch, but after rebasing it seems completely fixed. Be sure to double-check that. Before this can merge:
Note that at the time of writing, I haven't actually finished reviewing this yet, so there could be other things. I just wanted to let you know this was being worked on. |
Hi!
Thank you for working on it, everything seems reasonable, will do. I missed
checking 158, nice it is fixed!
Please give me few days to update.
…On Sat, 28 Apr 2018 at 06:02, Francesca Frangipane ***@***.***> wrote:
Alright, I'm back on macOS, so I can review this now. First of all, those
statistics look great!
I can still reproduce #158 <#158>
on this branch, *but* after rebasing it seems completely fixed. Be sure
to double-check that.
Before this can merge:
- examples/recreate_window_leak.rs needs to be removed, since it's not
useful for users. You can embed it in your PR description or as a gist if
you still want it to be accessible.
- This should have a CHANGELOG entry. I know it's not straightforward
to write one for something abstract like this, but a good overview of what
this improves will both A) make the next winit release seem cooler, and B)
give people an idea of what things to check if they experience regressions.
Note that at the time of writing, I haven't actually finished reviewing
this yet, so there could be other things. I just wanted to let you know
this was being worked on.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#481 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAEBGkIyTgmlxZRwgs6FiCpUUXe4Ro81ks5ts-nDgaJpZM4TfBSf>
.
|
as setDelegate:nil autoreleases WindowDelegate during work. Added NSAutoReleasepool for Window2::Create, as it uses autorelease on objects while doing work. Added NSAutoreleasepool for Window2::Drop as nswindow::close uses autorelease on objects. Added NSAutoreleasepool for IdRef. Moved Window2 WinitWindow objc class to a static var, as we are creating multiple windows.
c657566
to
69096b6
Compare
Rebased, added change log entry (not really sure it is a good one, please advise), checked the multiwindow example, this shows amount of alive window objects seems they are released as I close them this is how it looks like on a master: tried to repro 158 with multi desktops but could not. There is still some bytes leaked, namely some NSStrings and titles, but they are neglible in total memory usage, lets deal with them in the future. Can also confirm that this branch (without rebasing) alone did not fix 158 issue, which is interesting, |
@@ -94,7 +94,7 @@ impl Shared { | |||
if let Ok(mut windows) = self.windows.lock() { | |||
windows.retain(|w| match w.upgrade() { | |||
Some(w) => w.id() != id, | |||
None => true, | |||
None => false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont need to keep dead week ptrs here
src/platform/macos/window.rs
Outdated
@@ -452,9 +452,15 @@ impl WindowDelegate { | |||
unsafe { | |||
let delegate = IdRef::new(msg_send![WindowDelegate::class(), new]); | |||
|
|||
// setDelegate uses autorelease on objects, | |||
// so need autorelease | |||
let autoreleasepool = NSAutoreleasePool::new(cocoa::base::nil); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason you went with cocoa::base::nil
instead of using the imported nil
? It's a minor gripe, but having that consistent makes it easier for people to tell the same type is being used throughout.
Thanks. The CHANGELOG entry looks good.
Thanks for doing that. It gives me good peace of mind.
Prior to #476, windows in the multiwindow example weren't dropped until the program exited. Prior to #481, the drop implementation wasn't releasing windows correctly. With both, windows drop at the correct time and are dropped the right way. |
src/platform/macos/window.rs
Outdated
@@ -638,11 +679,30 @@ impl Window2 { | |||
} | |||
} | |||
|
|||
fn class() -> *const Class { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline
src/platform/macos/window.rs
Outdated
shared.find_and_remove_window(id); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline
src/platform/macos/window.rs
Outdated
None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, | ||
None => { | ||
let _: () = unsafe { msg_send![autoreleasepool, drain] }; | ||
return Err(OsError(format!("Couldn't create NSApplication"))); }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The },
should be on a newline. (This applies to the 2 matches below as well)
In terms of functionality, everything looks good. I pretty much just gave you some formatting nitpicks (sorry about those, but I'm hoping to get winit to look more cohesive internally). |
src/platform/macos/window.rs
Outdated
// Remove this window from the `EventLoop`s list of windows. | ||
// The destructor order is: | ||
// Window -> | ||
// Rc<Window2> (makes Weak<..> in shared.windows none) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be nice to have none
be None
, for clarity. We should be good to merge after this.
Fixed all code formatting errors, sorry about that, and thank you for all the work and explanation! |
Thanks! |
made multi window leak less memory
created a simple test which spawns windows and recreates them on "return" key press, the title of the window is the number of windows created.
before the change

after the change:

and more significant:
total persisted before:
and total persisted after
The gist of the change is the adding NSAutorelease pools, as in most places where we are calling objc it uses NSObject:autorelease which puts the object in the top most autorelease pool on the stack, or it is never released if no such autorelease pool exist,
or if the topmost autorelease pool is outside of the event loop, i.e. gets freed after the thread terminates.
This is continuation of #303 which was an incorrect attempt to do the same thing.
This probably fixes #158
I know its hard to check that without instruments and looking at the lists of allocations,
not sure how to best approach approving this pr