From 925bb43a6da211eed40523fc82f3308690ce3411 Mon Sep 17 00:00:00 2001 From: freedom7341 Date: Wed, 4 Oct 2023 13:02:03 -0600 Subject: [PATCH] fix memory calculation --- progmgr/group.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/progmgr/group.c b/progmgr/group.c index 9a90605..914c663 100644 --- a/progmgr/group.c +++ b/progmgr/group.c @@ -92,6 +92,7 @@ HWND CreateGroup(_In_ GROUP grp) HWND hWndGroup = NULL; HWND hWndListView = NULL; PGROUP pGroup = NULL; + UINT cbGroup = 0; // TODO: allocate memory for the group in the array // of group pointers in PGARRAY, then pass this to @@ -99,7 +100,9 @@ HWND CreateGroup(_In_ GROUP grp) // Unless... this just works a different way and I don't have to // keep track of all these stupid little pointers... since I can // just associate this with the appropriate window. - pGroup = (PGROUP)malloc(CalculateGroupMemory(&grp, 0)); + // calculate necessary group memory and create it + cbGroup = CalculateGroupMemory(&grp, 0); + pGroup = (PGROUP)malloc(cbGroup); // Get group minimized/maximized flags @@ -253,11 +256,14 @@ UINT CalculateGroupMemory(_In_ PGROUP pGroup, _In_ UINT cItems) // first add the size of a group strucutre cbGroupSize += sizeof(GROUP); - // calculate the total amount of items wanted - cItemBlock = pGroup->cItemArray + cItems; + // calculate the total amount of items wanted, set + // to 16 if there's less than 16 items so we always + // have some memory ready + cItemBlock = pGroup->cItemArray + cItems; + cItemBlock = cItemBlock > 16 ? cItemBlock : 16; // round the amount of items to the nearest but highest 16 - cItemBlock -= (cItems % 16) + 16; + cItemBlock = (cItemBlock / 16) * 16; // finally calculate the total group size cbGroupSize += cItemBlock * sizeof(ITEM);