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

Don't define the AVFoundation delegate more than once. #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

leecbaker
Copy link

@leecbaker leecbaker commented Aug 6, 2022

Fixes #25: This prevents a crash when multiple instances of the TTS engine are instantiated.

Here's what has changed: The delegate class (MyNSSpeechSynthesizerDelegate) should only be registered once; trying to register this a second time causes a crash. This class declaration is stored in a static variable, and reused.

The diff looks like much more code was much larger than it actually was, because the indentation level of the delegate class definition changed.

Code to demonstrate fix:

use tts::*;

#[cfg(target_os = "macos")]
use cocoa_foundation::base::id;
#[cfg(target_os = "macos")]
use cocoa_foundation::foundation::NSRunLoop;
#[cfg(target_os = "macos")]
use objc::{msg_send, sel, sel_impl};

fn main() {
    env_logger::init();

    let mut tts = Tts::default().unwrap();
    let mut tts2 = Tts::default().unwrap();

    tts.speak("Hello, world.", false).unwrap();
    tts2.speak("Hello, planet.", false).unwrap();
    tts.speak("Hello, again.", false).unwrap();

    #[cfg(target_os = "macos")]
    {
        let run_loop: id = unsafe { NSRunLoop::currentRunLoop() };
        unsafe {
            let _: () = msg_send![run_loop, run];
        }
    }
}

This prevents a crash when multiple instances of the TTS engine are instantiated.
@ndarilek
Copy link
Owner

So sorry I haven't gotten to this. Long story short, my apartment is a disaster and I've been displaced for weeks.

Related: normally I perform due diligence and make sure the examples run on my mac, but since I'm not at home for the time being, I can't do that. If you can confirm that the examples still work, which I assume you have but I want to be sure, then I'll merge this and cut a new release.

Thanks for debugging this, and again, sorry for the delay.

@ndarilek
Copy link
Owner

ping If I don't have confirmation, I won't be able to merge this until I get back home in a week or two (hopefully.) Some mac users depend on this crate and I'm pretty sure they're not hitting this due to only initializing once. Need to be absolutely sure it works for them and I can't do that myself now.

Thanks.

@leecbaker
Copy link
Author

Looks like I missed this comment earlier- sorry!

If you can confirm that the examples still work, which I assume you have but I want to be sure, then I'll merge this and cut a new release.

This did work on my mac, and does fix the issue with having multiple simultaneous instances; I've since run into another crash, this time with having multiple instances in sequence (i.e., initialize one instance, deinitialize it, then initialize a second without them existing simultaneously). I'm happy to wait to merge that until this problem is fixed as well.

In the long run, it might be best to have some types of tests run automatically, at least to verify that nothing crashes.

@ndarilek
Copy link
Owner

ndarilek commented Sep 12, 2022 via email

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

Successfully merging this pull request may close these issues.

Crash on initiailizing with AVFoundation backend
2 participants