Skip to content

Commit c408a02

Browse files
ann0seesoftins
andcommitted
Fix memory leak found by copilot
Co-authored-by: softins <softins@users.noreply.github.com> See: #3702
1 parent 76fc757 commit c408a02

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/clientdlg.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,7 @@ void CClientDlg::OnChatTextReceived ( QString strChatText )
876876
{
877877
if ( pSettings->bEnableAudioAlerts )
878878
{
879-
QSoundEffect* sf = new QSoundEffect();
880-
sf->setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_message.wav" ) );
881-
sf->play();
879+
PlayAudioAlert ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_message.wav" ) );
882880
}
883881
ChatDlg.AddChatText ( strChatText );
884882

@@ -927,9 +925,7 @@ void CClientDlg::OnNumClientsChanged ( int iNewNumClients )
927925
{
928926
if ( pSettings->bEnableAudioAlerts && iNewNumClients > iClients )
929927
{
930-
QSoundEffect* sf = new QSoundEffect();
931-
sf->setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_user.wav" ) );
932-
sf->play();
928+
PlayAudioAlert ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_user.wav" ) );
933929
}
934930

935931
// iNewNumClients will be zero on the first trigger of this signal handler when connecting to a new server.

src/clientdlg.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
#include <QMessageBox>
6161
#include <QFileDialog>
6262
#include <QActionGroup>
63-
#include <QSoundEffect>
6463
#if QT_VERSION >= QT_VERSION_CHECK( 5, 6, 0 )
6564
# include <QVersionNumber>
6665
#endif

src/util.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,3 +1679,16 @@ QString TruncateString ( QString str, int position )
16791679
}
16801680
return str.left ( position );
16811681
}
1682+
1683+
void PlayAudioAlert ( QUrl soundUrl )
1684+
{
1685+
QSoundEffect* sf = new QSoundEffect();
1686+
QObject::connect ( sf, &QSoundEffect::playingChanged, sf, [sf]() {
1687+
if ( !sf->isPlaying() )
1688+
{
1689+
sf->deleteLater();
1690+
}
1691+
} );
1692+
sf->setSource ( soundUrl );
1693+
sf->play();
1694+
}

src/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
#include <QElapsedTimer>
7676
#include <QTextBoundaryFinder>
7777
#include <QTimer>
78+
#include <QSoundEffect>
79+
7880
#ifndef DISABLE_SRV_DNS
7981
# include <QDnsLookup>
8082
#endif
@@ -1407,3 +1409,5 @@ struct EnumClassHash
14071409
}
14081410
};
14091411
#endif
1412+
1413+
void PlayAudioAlert ( QUrl soundUrl );

0 commit comments

Comments
 (0)