Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/clientdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,9 @@ void CClientDlg::OnChatTextReceived ( QString strChatText )
{
if ( pSettings->bEnableAudioAlerts )
{
QSoundEffect* sf = new QSoundEffect();
sf->setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_message.wav" ) );
sf->play();
QSoundEffect sf;
sf.setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_message.wav" ) );
sf.play();
Comment on lines +859 to +861
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be valid. Stack vs heap allocation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> memory leak

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this won't work. The original does indeed have a memory leak, as the pointer returned from new QSoundEffect() is only local, and disappears at the end of the block. But I think changing it so that the QSoundEffect sf is allocated locally on the stack may cause a crash, or at least truncate the playback, as the object will be destroyed immediately at the end of the block while the sound is still playing.

The correct solution is actually the one shown below around line 910: connect a lambda to the playingChanged signal and call deleteLater() when it is finished with.

}
ChatDlg.AddChatText ( strChatText );

Expand Down Expand Up @@ -907,7 +907,14 @@ void CClientDlg::OnNumClientsChanged ( int iNewNumClients )
{
if ( pSettings->bEnableAudioAlerts && iNewNumClients > iClients )
{
QSoundEffect* sf = new QSoundEffect();
QSoundEffect* sf = new QSoundEffect ( this );
connect ( sf, &QSoundEffect::playingChanged, this, [sf]()
{
if ( !sf->isPlaying() )
{
sf->deleteLater();
}
} );
Comment on lines +910 to +917
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ok and is the correct way to avoid the memory leak.

sf->setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_user.wav" ) );
sf->play();
}
Expand Down Expand Up @@ -1440,13 +1447,13 @@ void CClientDlg::SetMeterStyle ( const EMeterStyle eNewMeterStyle )
break;

case MT_BAR_NARROW:
lbrInputLevelL->SetLevelMeterType ( CLevelMeter::MT_BAR_WIDE );
lbrInputLevelR->SetLevelMeterType ( CLevelMeter::MT_BAR_WIDE );
lbrInputLevelL->SetLevelMeterType ( CLevelMeter::MT_BAR_NARROW );
lbrInputLevelR->SetLevelMeterType ( CLevelMeter::MT_BAR_NARROW );
break;

case MT_LED_ROUND_SMALL:
lbrInputLevelL->SetLevelMeterType ( CLevelMeter::MT_LED_ROUND_BIG );
lbrInputLevelR->SetLevelMeterType ( CLevelMeter::MT_LED_ROUND_BIG );
lbrInputLevelL->SetLevelMeterType ( CLevelMeter::MT_LED_ROUND_SMALL );
lbrInputLevelR->SetLevelMeterType ( CLevelMeter::MT_LED_ROUND_SMALL );
break;
}
Comment thread
ann0see marked this conversation as resolved.

Expand Down
Loading