freesig / cpal

Audio player in pure Rust
0 stars 0 forks source link

ASIOExit crash #29

Closed freesig closed 6 years ago

freesig commented 6 years ago

Problem

Calling ASIOExit crashes the program. Even if ASIOInit has been called. I thought this might be due to the asio4all drivers but I just confirmed this is happening on dante as well. It wasn't happening before so it must be something that I have changed in the last few commits

freesig commented 6 years ago

So I looked at the old code and I was never actually calling ASIOExit before. Instead I was calling removeCurrentDriver() But if you look at the code it doesn't seem to do anything else (note the windows macro) but call removeCurrentDriver() and set the theAsioDriver to 0. The only thing I cant think of is that it's calling removeCurrentDriver() on the asioDrivers but that class is constructed on the rust side of the ffi. It's declared like this in c++: extern AsioDrivers *asioDrivers;

ASIOError ASIOExit(void)
{
    if(theAsioDriver)
    {
#if WINDOWS
        asioDrivers->removeCurrentDriver();
#else
        delete theAsioDriver;
#endif
    }       
    theAsioDriver = 0;
    return ASE_OK;
}
void AsioDrivers::removeCurrentDriver()
{
    if(curIndex != -1)
        asioCloseDriver(curIndex);
    curIndex = -1;
}
freesig commented 6 years ago

OK so I found this function

bool loadAsioDriver(char *name)
{
    if(!asioDrivers)
        asioDrivers = new AsioDrivers();
    if(asioDrivers)
        return asioDrivers->loadDriver(name);
    return false;
}

This is how you are meant to use it

// some external references
extern AsioDrivers* asioDrivers;
bool loadAsioDriver(char *name);

It looks like you have to redeclare it in your file with the extern keyword. But I have no idea how to do this across an FFI.

For now I will just avoid ASIOExit and manually handle the asiodriver but I won't close this issue until someone reviews this. I'm pretty sure it won't make any difference but it would be nice if we could do it the right way.

freesig commented 6 years ago

Actually never mind I fixed it using a cpp helper