?
Solved

Why is GlobalAlloc etc not freed???

Posted on 2004-11-02
2
Medium Priority
?
333 Views
Last Modified: 2013-12-03
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;
0
Comment
Question by:djwyatt2
2 Comments
 
LVL 11

Accepted Solution

by:
KurtVon earned 260 total points
ID: 12477316
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
 

Author Comment

by:djwyatt2
ID: 12477715
Yep,it sure does help out alot KurtVon.Thank you once again.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question