Why is GlobalAlloc etc not freed???

take this code here.If I use getwintext+=1 to step through the string the GlobalFree((HANDLE)getwintext); does not free it unless I reset getwintext to getwintext-=1;


Code below does not free the memory.
    case WM_KEYDOWN : {
    switch(LOWORD(wParam)) {
    case VK_RETURN : {
     
     int len = GetWindowTextLength(edit);
     char *getwintext;
       
     
   if(len > 0){
       
     if((getwintext = (char*)GlobalAlloc(GPTR, len + 1))!= NULL)
     {
     GetWindowText(edit, getwintext, len +1);
     getwintext+=1;
 
     GlobalFree((HANDLE)getwintext);
     MessageBox(NULL,getwintex,  "Error!",MB_ICONEXCLAMATION | MB_OK);
     }    
     
     }
     }break;
     }
     }break;




Code below does free the memory.
    case WM_KEYDOWN : {
    switch(LOWORD(wParam)) {
    case VK_RETURN : {
     
     int len = GetWindowTextLength(edit);
     char *getwintext;
       
     
   if(len > 0){
       
     if((getwintext = (char*)GlobalAlloc(GPTR, len + 1))!= NULL)
     {
     GetWindowText(edit, getwintext, len +1);
     getwintext+=1;
     getwintext-=1;  
     GlobalFree((HANDLE)getwintext);
     MessageBox(NULL,getwintex,  "Error!",MB_ICONEXCLAMATION | MB_OK);
     }    
     
     }
     }break;
     }
     }break;
djwyatt2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KurtVonCommented:
GlobalAlloc allocates a memory handle, not a memory pointer.  Because of that the handle needs to be treated as a transparent object.  You are not allowed to change the value directly, which is why adding one to it changes it so it cannot be freed.  The GlobalFree must be passed the same handle that GlobalAlloc returned.

And, by the way, you cannot store data at the handle as if it were a pointer.  Technically it is, but you are not allowed to assume that because Windows is allowed to relocate the memory during normal memory cleaning operations.  There is no guarantee the memory pointed to is going to be the same, even if the handle is unchanged.

To get the memory location you need to use GlobalLock, which returns a pointer to the memory location associated with the handle.  Call GlobalUnlock to let Windows shuffle the memory around again (in other words, the pointer from GlobalLock will no longer be valid after the GlobalUnlock).  The big advantage to this is that you can allocate huge chunks of memory without making your program cause problems for other programs that may also need that memory.  Windows knows when you are really accessing the memory, and if you don't immediately need it it knows it can cache it to disk.

Hope this helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
djwyatt2Author Commented:
Yep,it sure does help out alot KurtVon.Thank you once again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.