Skip to content

Commit

Permalink
Prevent crash after idling 5 hours on main menu (#3903)
Browse files Browse the repository at this point in the history
mitigated a music related memory leak
  • Loading branch information
xtremeqg authored Feb 24, 2025
1 parent d39ab4a commit 18f0d48
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
33 changes: 16 additions & 17 deletions src/front_landview.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,20 @@ void frontnetmap_unload(void)
set_music_volume(settings.music_volume);
}

static void frontmap_start_music(void)
{
if (strlen(campaign.soundtrack_fname) > 0) {
const int track = atoi(campaign.soundtrack_fname);
if (track >= 1) {
play_music_track(track);
} else {
play_music(campaign.soundtrack_fname);
}
} else {
play_music_track(2);
}
}

TbBool frontnetmap_load(void)
{
SYNCDBG(8,"Starting");
Expand Down Expand Up @@ -988,6 +1002,7 @@ TbBool frontnetmap_load(void)
map_sound_fade = 256;
lbDisplay.DrawFlags = 0;
set_music_volume(settings.music_volume);
frontmap_start_music();
if (fe_network_active)
{
net_number_of_players = 0;
Expand Down Expand Up @@ -1078,20 +1093,6 @@ TbBool frontmap_update_zoom(void)
return false;
}

static void frontmap_start_music(void)
{
if (strlen(campaign.soundtrack_fname) > 0) {
const int track = atoi(campaign.soundtrack_fname);
if (track >= 1) {
play_music_track(track);
} else {
play_music(campaign.soundtrack_fname);
}
} else {
play_music_track(2);
}
}

TbBool frontmap_load(void)
{
SYNCDBG(4,"Starting");
Expand Down Expand Up @@ -1124,7 +1125,6 @@ TbBool frontmap_load(void)
return false;
}
frontend_load_data_reset();
frontmap_start_music();
struct PlayerInfo* player = get_my_player();
lvnum = get_continue_level_number();
if ((player->flgfield_6 & PlaF6_PlyrHasQuit) != 0)
Expand Down Expand Up @@ -1154,6 +1154,7 @@ TbBool frontmap_load(void)
play_non_3d_sample(campaign.ambient_bad);
}
set_music_volume(settings.music_volume);
frontmap_start_music();
fe_computer_players = 0;
update_ensigns_visibility();
SYNCDBG(7,"Finished");
Expand Down Expand Up @@ -1765,8 +1766,6 @@ TbBool frontnetmap_update(void)
if (!fe_network_active)
fe_computer_players = 1;
}

play_music_track(2);
SYNCDBG(8,"Normal end");
return false;
}
Expand Down
53 changes: 49 additions & 4 deletions src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,7 @@ FrontendMenuState frontend_setup_state(FrontendMenuState nstate)
set_pointer_graphic_none();
break;
case FeSt_MAIN_MENU:
stop_music();
continue_game_option_available = continue_game_available();
if (!continue_game_option_available)
{
Expand Down Expand Up @@ -2794,6 +2795,7 @@ FrontendMenuState frontend_setup_state(FrontendMenuState nstate)
credits_end = 0;
LbTextSetWindow(0, 0, lbDisplay.PhysicalScreenWidth, lbDisplay.PhysicalScreenHeight);
lbDisplay.DrawFlags = Lb_TEXT_HALIGN_CENTER;
play_music_track(7);
break;
case FeSt_LEVEL_STATS:
turn_on_menu(GMnu_FESTATISTICS);
Expand All @@ -2820,6 +2822,7 @@ FrontendMenuState frontend_setup_state(FrontendMenuState nstate)
turn_on_menu(GMnu_FEDEFINE_KEYS);
break;
case FeSt_FEOPTIONS:
play_music_track(3);
turn_on_menu(GMnu_FEOPTION);
set_pointer_graphic_menu();
break;
Expand Down Expand Up @@ -2852,14 +2855,59 @@ FrontendMenuState frontend_setup_state(FrontendMenuState nstate)
return nstate;
}

static const char * menu_state_str(FrontendMenuState state)
{
switch (state) {
case FeSt_INITIAL: return "FeSt_INITIAL";
case FeSt_MAIN_MENU: return "FeSt_MAIN_MENU";
case FeSt_FELOAD_GAME: return "FeSt_FELOAD_GAME";
case FeSt_LAND_VIEW: return "FeSt_LAND_VIEW";
case FeSt_NET_SERVICE: return "FeSt_NET_SERVICE";
case FeSt_NET_SESSION: return "FeSt_NET_SESSION";
case FeSt_NET_START: return "FeSt_NET_START";
case FeSt_START_KPRLEVEL: return "FeSt_START_KPRLEVEL";
case FeSt_START_MPLEVEL: return "FeSt_START_MPLEVEL";
case FeSt_UNKNOWN09: return "FeSt_UNKNOWN09";
case FeSt_LOAD_GAME: return "FeSt_LOAD_GAME";
case FeSt_INTRO: return "FeSt_INTRO";
case FeSt_STORY_POEM: return "FeSt_STORY_POEM";
case FeSt_CREDITS: return "FeSt_CREDITS";
case FeSt_DEMO: return "FeSt_DEMO";
case FeSt_UNUSED1: return "FeSt_UNUSED1";
case FeSt_UNUSED2: return "FeSt_UNUSED2";
case FeSt_LEVEL_STATS: return "FeSt_LEVEL_STATS";
case FeSt_HIGH_SCORES: return "FeSt_HIGH_SCORES";
case FeSt_TORTURE: return "FeSt_TORTURE";
case FeSt_UNKNOWN20: return "FeSt_UNKNOWN20";
case FeSt_OUTRO: return "FeSt_OUTRO";
case FeSt_UNKNOWN22: return "FeSt_UNKNOWN22";
case FeSt_UNKNOWN23: return "FeSt_UNKNOWN23";
case FeSt_NETLAND_VIEW: return "FeSt_NETLAND_VIEW";
case FeSt_PACKET_DEMO: return "FeSt_PACKET_DEMO";
case FeSt_FEDEFINE_KEYS: return "FeSt_FEDEFINE_KEYS";
case FeSt_FEOPTIONS: return "FeSt_FEOPTIONS";
case FeSt_UNKNOWN28: return "FeSt_UNKNOWN28";
case FeSt_STORY_BIRTHDAY: return "FeSt_STORY_BIRTHDAY";
case FeSt_LEVEL_SELECT: return "FeSt_LEVEL_SELECT";
case FeSt_CAMPAIGN_SELECT: return "FeSt_CAMPAIGN_SELECT";
case FeSt_DRAG: return "FeSt_DRAG";
case FeSt_CAMPAIGN_INTRO: return "FeSt_CAMPAIGN_INTRO";
case FeSt_MAPPACK_SELECT: return "FeSt_MAPPACK_SELECT";
case FeSt_FONT_TEST: return "FeSt_FONT_TEST";
}
return "unknown";
}

FrontendMenuState frontend_set_state(FrontendMenuState nstate)
{
SYNCDBG(8,"State %d will be switched to %d",(int)frontend_menu_state,(int)nstate);
frontend_shutdown_state(frontend_menu_state);
if ( frontend_menu_state )
fade_out();
fade_palette_in = 1;
SYNCMSG("Frontend state change from %d into %d",(int)frontend_menu_state,(int)nstate);
SYNCMSG("Frontend state change from %d (%s) into %d (%s)",
frontend_menu_state, menu_state_str(frontend_menu_state),
nstate, menu_state_str(nstate));
frontend_menu_state = frontend_setup_state(nstate);
return frontend_menu_state;
}
Expand Down Expand Up @@ -3510,7 +3558,6 @@ void frontend_update(short *finish_menu)
switch ( frontend_menu_state )
{
case FeSt_MAIN_MENU:
stop_music();
frontend_button_info[8].font_index = (continue_game_option_available?1:3);
//this uses original timing function for compatibility with frontend_set_state()
if ( abs(LbTimerClock()-(long)time_last_played_demo) > MNU_DEMO_IDLE_TIME )
Expand Down Expand Up @@ -3544,7 +3591,6 @@ void frontend_update(short *finish_menu)
exit_keeper = 1;
break;
case FeSt_CREDITS:
play_music_track(7);
break;
case FeSt_LEVEL_STATS:
frontstats_update();
Expand All @@ -3556,7 +3602,6 @@ void frontend_update(short *finish_menu)
*finish_menu = frontnetmap_update();
break;
case FeSt_FEOPTIONS:
play_music_track(3);
break;
case FeSt_LEVEL_SELECT:
frontend_level_select_update();
Expand Down

0 comments on commit 18f0d48

Please sign in to comment.