NO

Author Topic: How do I free this allocated memory?  (Read 5360 times)

x79

  • Guest
How do I free this allocated memory?
« on: March 13, 2015, 12:50:19 AM »
Here's the situation:

I declare SHELLEXECUTEINFOW rInfo. In a nested loop, I need to set rInfo..lpFile to a wide string so I declare a temp wchar_t array and wcscpy the data to it. Now if I try to wcscpy the temp array to the rInfo.lpFile, I get a 'const' error in the wcscpy function (expected wchar_t * but received const wchar_t *). So instead, set rInfo.lpFile = the temp wchar_t array.

Here is where I have a problem. I used HeapAlloc to create the temp array. If I HeapFree the temp array after I point the rInfo.lpFile to it, the memory gets overwritten/corrupted. If I try to HeapFree the temp array outside of the loop, I get an error (undeclared variable or something like that). Do I need to declare the temp variable before the loop or do I need to free rInfo.lpFile or is there a better solution?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How do I free this allocated memory?
« Reply #1 on: March 13, 2015, 04:40:55 PM »
To be honest I understood almost nothing  ;D
You created a temporary array of wide chars and copied in it your data.
Then you want copy the contents of this temporary array to the lpFile member of a SHELLEXECUTEINFO structure ?
You cannot because it is declared as constant?
That is absolutely wrong, the SHELLEXECUTEINFO structure lpFile member is a pointer not a buffer, you don't have to copy data there, but assign to it your buffer:
Code: [Select]
struct SHELLEXECUTEINFO  sei;
....
sei.lpFile = wszMy§AllocatedDataArray;
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

x79

  • Guest
Re: How do I free this allocated memory?
« Reply #2 on: March 13, 2015, 05:15:49 PM »
sorry, didn't want to paste entire project and I can't right now from my phone.
I assign the temp array to lpFile just fine. I got the const error when I tried to pass lpFile as the dest in StringCchCopy()

So what I do now is HeapAlloc the temp array in the nested loop then point lpFile to that. From there I can't free the temp array. Do I need to just free lpFile when I'm done with it or the whole SHELLEXECUTEINFO structure?

This should be more clear
Code: [Select]
// Code here to open ini file and save the text in sText

// Now we should have a valid UCS-16 LE string in sText
SHELLEXECUTEINFOW rInfo = {0};
rInfo.cbSize = 60;
rInfo.nShow = 1;

while (*sEndPtr)   // Loop while the end of line pointer doesn't point to \0

    // Code her to get a line of text from sText and store the substrings into sSetting and sLine
    if (wcscmp(sSetting, L"lpFile") == 0) {
        // I can malloc an array and point rInfo.lpFile to it here but
        //   if I try to do this
        //   wcscpy(rInfo.lpFile, sLine);
        //   I get an error that wcscpy expected wchar_t * but received const wchar_t *

        // Instead, I malloc a temp array and point rInfo.lpFile to it
        wchar_t *sTemp = malloc(sizeof(wchar_t) * iSize);
        wcscpy(sTemp, sLine);
        rInfo.lpFile = sTemp;

        // If I try to free(sTemp) here it works but
        //   when I try to read from rInfo.lpFile later, it is overwritten/corrupted
    } // end if
} // end while

ShellExecuteExW(rInfo);

// Now that I'm done with rInfo I can free that memory
// If I try to free sTemp here, I get an error that it is undeclared or undefined or whatever
// To properly clean it up, do I need to free(rInfo.lpFile) or simply free(rInfo) or is there something else I'm missing?
« Last Edit: March 13, 2015, 06:26:14 PM by x79 »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How do I free this allocated memory?
« Reply #3 on: March 13, 2015, 09:44:49 PM »
Code: [Select]
wcscpy((WCHAR *)rInfo.lpFile, sLine);          //Simply use a cast

Code: [Select]
while(...)
{
        .....
        wchar_t *sTemp = malloc(sizeof(wchar_t) * iSize);     //you are declaring your variable in a scope block
        .....
}
free(sTemp);         //You are out of scope, sTemp doesn't exist anymore, the dynamic memory pointed by it is lost
Solution:
Code: [Select]
wchar_t *sTemp;      //Declare variable in outer scope
while(...)
{
        .....
        sTemp = malloc(sizeof(wchar_t) * iSize);     //you are only using the variable
        .....
}
free(sTemp);         //Now works
« Last Edit: March 13, 2015, 09:53:58 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

x79

  • Guest
Re: How do I free this allocated memory?
« Reply #4 on: March 14, 2015, 04:12:00 AM »
Code: [Select]
wcscpy((WCHAR *)rInfo.lpFile, sLine);          //Simply use a cast
:o Why didn't I think of that?

Ok, so now what I'm doing is
Code: [Select]
SHELLEXECUTEINFOW rInfo = {0};
while(...)
{
    ....
    rInfo.lpFile = HeapAlloc(pMain, 8ul, (iCmdLen)*4);
    StringCchCopyW((wchar_t *)rInfo.lpFile, iCmdLen, sStartPtr);
    ....
}

// do I need to do this?
HeapFree(pMain, 0ul, rInfo.lpFile);

// or just this?
HeapFree(pMain, 0ul, rInfo);

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How do I free this allocated memory?
« Reply #5 on: March 14, 2015, 05:02:15 PM »
rInfo is statically allocated, so you don't have to free it, and if you try you'll get an error.
The pointer to the memory allocated has been saved in the lpFile member of the statiically allocated rInfo, so you have to pass this poinnter to free the memory:
Code: [Select]
HeapFree(pMain, 0ul, rInfo.lpFile);
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

x79

  • Guest
Re: How do I free this allocated memory?
« Reply #6 on: March 14, 2015, 09:10:24 PM »
tyvm  :)