Skip to content

Commit

Permalink
* Fix memory leak in options menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
iProgramMC committed May 29, 2024
1 parent b1ba252 commit c8ba737
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/windows/OptionsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ DLGTEMPLATEEX; // huh??

struct DialogHeader
{
HWND hwndTab; // tab control
HWND hwndDisplay; // current child dialog box
RECT rcDisplay;
DLGTEMPLATEEX* apRes[C_PAGES];
int pageNum;
HWND hwndTab = NULL; // tab control
HWND hwndDisplay = NULL; // current child dialog box
RECT rcDisplay{};
DLGTEMPLATEEX* apRes[C_PAGES] = { NULL };
int pageNum = 0;
};

void AddTab(HWND hwndTab, TCITEM& tie, int index, LPTSTR str)
Expand Down Expand Up @@ -432,14 +432,15 @@ HRESULT OnPreferenceDialogInit(HWND hWnd)
int cxMargin = LOWORD(dwDlgBase) / 4;
int cyMargin = HIWORD(dwDlgBase) / 8;

DialogHeader* pHeader = (DialogHeader*)LocalAlloc(LPTR, sizeof(DialogHeader)); // not sure why they're using LocalAlloc here

DialogHeader* pHeader = new DialogHeader;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pHeader);

// Get the tab control. Note the hardcoded sizes:
HWND hwndTab = GetDlgItem(hWnd, IDC_OPTIONS_TABS);
if (!hwndTab)
if (!hwndTab) {
delete pHeader;
return HRESULT_FROM_WIN32(GetLastError());
}

pHeader->hwndTab = hwndTab;

Expand Down Expand Up @@ -523,8 +524,15 @@ static INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
LPNMHDR hdr = (LPNMHDR)lParam;
return OptionsOnNotify(hdr->hwndFrom, hWnd, lParam);
}
case WM_CLOSE:
case WM_DESTROY:
{
DialogHeader* pData = (DialogHeader*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (pData)
delete pData;

// fallthrough
}
case WM_CLOSE:
{
EndDialog(hWnd, OPTIONS_RESULT_OK);
break;
Expand Down

0 comments on commit c8ba737

Please sign in to comment.