Crash with new AudioEngine code in v3.15

Crash with new AudioEngine code in v3.15
0.0 0

#1

I just updated a project to cocos2d-x 3.15, and noticed that AppDelegate.cpp now has the following code:

AppDelegate::~AppDelegate() 
{
#if USE_AUDIO_ENGINE
    AudioEngine::end();
#elif USE_SIMPLE_AUDIO_ENGINE
    SimpleAudioEngine::end();
#endif
}

USE_AUDIO_ENGINE is defined, so it’s calling “AudioEngine::end();” in the AppDelegate destructor, but when it does so, it crashes.

AudioEngine::end() does this “delete _audioEngineImpl;”, then in “AudioEngineImpl::~AudioEngineImpl”, it does the following:

if (_scheduler != nullptr)
{
    _scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
}
....

This is where the crash happens, in CCScheduler.cpp, function Scheduler::unschedule:

void Scheduler::unschedule(SEL_SCHEDULE selector, Ref *target)
{
    // explicit handle nil arguments when removing an object
    if (target == nullptr || selector == nullptr)
    {
        return;
    }
    
    tHashTimerEntry *element = nullptr;
    HASH_FIND_PTR(_hashForTimers, &target, element);  <= crashes right here, with "_hashForTimers" having an invalid value (such as 0xDDDDDDDD).

I have absolutely no idea why this is happening, and will look into it further, but just on a quick glance I don’t actually know what this section of code does, nor why _hashForTimers would contain an invalid value.

Before I log this in as an issue, has anyone else experienced this crash, or have an idea why it may be happening?

It is reproducible, and happens any time I close the game application.


#2

Never mind! I took a guess and it turned out to be right. The scheduler is being deleted when the Director instance is deleted, so the scheduler instance is no longer valid. The AudioEngine::end() ends up calling the AudioEngineImpl destructor, which has a reference to the scheduler, but since the scheduler has already been deleted, it is pointing to invalid memory. That is why the crash is happening in “Scheduler::unschedule”.

I’ll check this in the issue tracker.


#3

今天剛從官網下載最新版cocos2d-x-3.15.1問題還是依舊,調適代碼iphone按home,director==null 雖然沒崩潰,但聲音並未pause


#4

Is there a fix for this? I’m using cocos2d-x 3.16, and I’m getting this error in my Windows project, after I switched to AudioEngine. Other than this issue, AudioEngine is running great.