Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(milesaudiomanager): stop a paused game leaking the sounds it dropped

Olcay Seygand19d5e929fa2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 81494ec43..e12f2bc42 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -145,6 +145,8 @@ found and fixed — EA's own, not port damage.**
- Somebody quitting no longer reports a desync to everyone still playing.
- A power sabotage no longer follows a player into the next match.
- Muting sound effects or speech no longer desyncs the game.
+- Pausing no longer leaks a little memory for every sound that was about to start.
+- A sound cancelled before it started really is cancelled. The check compared a pointer against a handle - two different things sharing one slot - so it never matched and the sound played anyway.
- Angles come from the game's own table, identical on every machine.
- Defeat, second maps and the sync fingerprint each had a drift bug; fixed.
- The mismatch check now covers whole units and all sixty-eight special powers.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/AudioRequest.h b/GeneralsMD/Code/GameEngine/Include/Common/AudioRequest.h
index 730a49542..2fd52984b 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/AudioRequest.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/AudioRequest.h
@@ -56,6 +56,10 @@ public:
};
Bool m_usePendingEvent;
Bool m_requiresCheckForSample;
+
+ /** A play request owns the event it is carrying until something takes it - the destructor frees
+ * whatever is left, so a request dropped without being processed no longer leaks it. */
+ AudioEventRTS *releasePendingEvent();
};
#endif // _AUDIOREQUEST_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioRequest.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioRequest.cpp
index c4e178840..2325f0895 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioRequest.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioRequest.cpp
@@ -30,5 +30,25 @@
AudioRequest::~AudioRequest()
{
-
+ // A play request carries an event it allocated. Pausing the game throws every pending play
+ // request away, and killing a sound before it starts throws one away, and neither freed the
+ // event - so every pause leaked one allocation per queued sound.
+ if (m_usePendingEvent)
+ {
+ delete m_pendingEvent;
+ m_pendingEvent = NULL;
+ m_usePendingEvent = FALSE;
+ }
+}
+
+AudioEventRTS *AudioRequest::releasePendingEvent()
+{
+ if (m_usePendingEvent)
+ {
+ AudioEventRTS *event = m_pendingEvent;
+ m_pendingEvent = NULL;
+ m_usePendingEvent = FALSE;
+ return event;
+ }
+ return NULL;
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
index 3cf959eaa..ff86dd9a7 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
@@ -663,8 +663,13 @@ void MilesAudioManager::playAudioEvent( AudioEventRTS *event )
#ifdef INTENSIVE_AUDIO_DEBUG
DEBUG_LOG(("MILES (%d) - Processing play request: %d (%s)", TheGameLogic->getFrame(), event->getPlayingHandle(), event->getEventName().str()));
#endif
+ if (!event) {
+ return;
+ }
+
const AudioEventInfo *info = event->getAudioEventInfo();
if (!info) {
+ delete event; // nothing downstream will take it now
return;
}
@@ -981,7 +986,11 @@ void MilesAudioManager::killAudioEventImmediately( AudioHandle audioEvent )
for( ait = m_audioRequests.begin(); ait != m_audioRequests.end(); ait++ )
{
AudioRequest *req = (*ait);
- if( req && req->m_request == AR_Play && req->m_handleToInteractOn == audioEvent )
+ // m_handleToInteractOn and m_pendingEvent share one union, and a play request fills in the
+ // event pointer - so this compared a pointer against a handle, never matched, and a sound
+ // killed before it started played anyway.
+ if( req && req->m_usePendingEvent && req->m_pendingEvent
+ && req->m_pendingEvent->getPlayingHandle() == audioEvent )
{
req->deleteInstance();
ait = m_audioRequests.erase(ait);
@@ -2943,7 +2952,9 @@ void MilesAudioManager::processRequest( AudioRequest *req )
{
case AR_Play:
{
- playAudioEvent(req->m_pendingEvent);
+ // hand the event over: from here it belongs to the playing list, or to playAudioEvent
+ // to free if it cannot be played
+ playAudioEvent(req->releasePendingEvent());
break;
}
case AR_Pause: