From d813c3ae377c046002f640077993f025e1dc77f7 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 24 Apr 2024 20:05:26 -0400 Subject: [PATCH] More modern memory management in the modern pstarmap file. --- sc2/src/uqm/planets/pstarmap-modern.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/sc2/src/uqm/planets/pstarmap-modern.cpp b/sc2/src/uqm/planets/pstarmap-modern.cpp index 4033feb1d..d636aa797 100644 --- a/sc2/src/uqm/planets/pstarmap-modern.cpp +++ b/sc2/src/uqm/planets/pstarmap-modern.cpp @@ -18,6 +18,7 @@ */ #include +#include #include "../starmap.h" #include "../menustat.h" @@ -28,22 +29,14 @@ extern "C" bool DoStarSearch (MENU_STATE *pMS) +try { TEXTENTRY_STATE tes; - STAR_SEARCH_STATE *pss; bool success; - pss = (STAR_SEARCH_STATE *) HMalloc (sizeof (*pss)); - if (!pss) - return FALSE; - + auto pss= std::make_unique< STAR_SEARCH_STATE >(); std::vector< int > sorted( GetStarCount() ); // Manages the sorted stars memory. pss->SortedStars= sorted.data(); - if (!pss->SortedStars) - { - HFree (pss); - return FALSE; - } DrawSISMessageEx ("", 0, 0, DSME_SETFR); @@ -52,14 +45,14 @@ DoStarSearch (MENU_STATE *pMS) pss->Text[0] = '\0'; pss->LastText[0] = '\0'; pss->FirstIndex = -1; - SortStarsOnName (pss); + SortStarsOnName (pss.get()); // text entry setup tes.Initialized = FALSE; tes.BaseStr = pss->Text; tes.MaxSize = sizeof (pss->Text); tes.CursorPos = 0; - tes.CbParam = pss; + tes.CbParam = pss.get(); tes.ChangeCallback = OnStarNameChange; tes.FrameCallback = OnStarNameFrame; @@ -69,7 +62,9 @@ DoStarSearch (MENU_STATE *pMS) DrawSISMessageEx (pss->Text, -1, -1, DSME_CLEARFR); - HFree (pss); - return success; } +catch( const std::bad_alloc & ) +{ + return FALSE; +}