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

When is it required to use the autoreleasepool? #623

Open
ghostman2013 opened this issue May 28, 2024 · 2 comments
Open

When is it required to use the autoreleasepool? #623

ghostman2013 opened this issue May 28, 2024 · 2 comments
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates question Further information is requested

Comments

@ghostman2013
Copy link

Hello!

I am a little confused with a Rust implementation of autoreleasepool... When I type a something as below:

fn func(mtm: MainThreadMaker) -> Id<NSSomeObject> {
    autoreleasepool(|_| {
        let alloc_1 = mtm.alloc();
        let some_object: Id<NSSomeObject> = NSSomeObject::alloc(alloc_1);
        let alloc_2 = mtm.alloc();
        let child_object: Id<NSChildObject> = NSChildObject::alloc(alloc_2);

       some_object.set_child(&child_object); // Replaced an old automatically-created child on Obj C side.

       some_object
    })
}

What will happen? Does autoreleasepool release the old automatically-created child on Obj C side? Or will it delete child_object too?

P.S. Sorry for the noobie's question, I am not a Objective C programmer, the autoreleasepool is like a black box for me.

@ghostman2013
Copy link
Author

To be clearer...

fn set_icon(&self, icon: Option<&Icon>) {
    autoreleasepool(|_| {
        let mtm = MainThreadMarker::from(self);
        let app = NSApp(mtm);
        match icon {
            Some(icon) => {
                let ns_image = icon.get_impl().get_native();
                unsafe { app.setApplicationIconImage(Some(&ns_image)) };
            }
            None => unsafe { app.setApplicationIconImage(None) },
        }
    });
}

Should I use the autoreleasepool in such cases? There can be an old application icon image as I get.

@madsmtm madsmtm added question Further information is requested A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels May 28, 2024
@madsmtm
Copy link
Owner

madsmtm commented May 31, 2024

I think that by understanding autorelease pools in Objective-C (builtin @autoreleasepool), you'd get a lot of the way there to understanding it in Rust. There's a link to some docs here, I've added a link to that in 2066b7f.

In short, you use it mostly to reduce the peak memory footprint, it shouldn't affect safety or soundness in any way.

Specifically for setApplicationIconImage, the answer is that it's implementation-defined whether it releases the previous image into the autorelease pool or not (in reality, it's unlikely that it will, but it might have done so in the past, or might do so in the future).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants