Use of global variables in Dialog procedure

This is a discussion on Use of global variables in Dialog procedure within the Windows Programming forums, part of the Platform Specific Boards category; I am trying to set some Edit boxes with SetDlgItemText() in WM_INITDIALOG in a Dialog Box. I have two pointers ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    44

    Use of global variables in Dialog procedure

    I am trying to set some Edit boxes with SetDlgItemText() in WM_INITDIALOG in a Dialog Box. I have two pointers to wide character strings, buf and buf1, which are defined as global variables. Buf I fill with a GetDlgItemText command and buf1 I fill with a function I defined in some separate C file. Both strings I can print with the MessageBox line (so it doesn't matter if I use buf or buf1), but if I try to fill some Edit boxes with them, it only shows the correct text for buf and for buf1 I get the strange characters. I can't see what I am doing differently. The commands in the WM_INITDIALOG are exactly the same. The code where buf and buf1 are initialized in the OK button case of the same Dialog procedure. Does anybody understand what is going on?


    Code:
    case IDOK:
    				{
    					len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT1));
    					if(len > 0)
    					{   
    						buf = (LPTSTR)GlobalAlloc(GPTR, 2*(len + 1));
    						buf1 = (LPWSTR)GlobalAlloc(GPTR, 1024);
    					        GetDlgItemText(hwnd, IDC_EDIT1, buf, 2*(len + 1));
    						buf1 = Calculate(hwnd, buf, buf1, len);
    
    						MessageBox(hwnd, buf1, (LPCTSTR)L"Warning!", MB_OK | MB_ICONINFORMATION);
    
    				return TRUE;
    				}
    			break;

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Looks like Calculate() is returning garbage, that's all. The only other reason the contents of buf1 could be "corrupted" is if some other thread interfered, or if multiple instances of the dialog were active simultaneously. The use of a global variable here is really awful.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    The problem is that I need to access these strings outside the Dialog procedure so I don't really see how I can do that without global variables.
    Calculate() is not returning garbage since I can simple print it in the MessageBox. I solved the problem, but I don't really understand why it works. I renamed buf1 into buf2 and copied the contents of buf2 to buf1 by using wsprintf. Then it worked fine, but I have no clue why...

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    This line...

    Code:
    buf1 = Calculate(hwnd, buf, buf1, len);
    Are you sure that Calculate() is returning the right pointer? It might be filling in buf1 correctly, but returning a bogus pointer?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    I wouldn't know how to tell if a pointer is no good. Is there such thing as bogus pointers?

  6. #6
    Registered User KyussRyn's Avatar
    Join Date
    Mar 2009
    Location
    Tokyo, Japan
    Posts
    7
    Code:
    buf = (LPTSTR)GlobalAlloc(GPTR, 2*(len + 1));
    buf1 = (LPWSTR)GlobalAlloc(GPTR, 1024);
    I have just had a quick look and I wont say I am any good with c++ but you are using LPTSTR and LPWSTR castings here, have you tried using the same casting here?
    Last edited by KyussRyn; 02-24-2010 at 02:31 AM. Reason: Added code tags

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    I noticed I've been mixing them, but since LPTSTR is already cast to LPWSTR if the Unicode variable is set, it doesn't matter. thanx for mentioning it though.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    I don't understand why you are passing buf1 into calculate, but you are then assigning the return value to buf1. What exactly is going on inside Calculate with buf1?

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Why pass buf1 to Calculate() as input and also as a return?

    Is there some reason you are not testing the 2 buffers to see if they have already had memory alloced previously? (which would be a memory leak)

    Quote Originally Posted by mvanrijnbach View Post
    I wouldn't know how to tell if a pointer is no good. Is there such thing as bogus pointers?
    If Calculate() returns a pointer to a local variable then you have a 'dangling' pointer.

    This is because the local variable only exists (has scope) while the Calculate() function executes.

    The memory may hold the correct value for some time, hiding the problem (especially in debug mode within the IDE).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    yeah, you might be right that I was probably using a local pointer. I am not really sure anymore, because I have rewritten the procedure completely since last week. Thanx anyway for pointing out that a local pointer might be the problem. The fact that I could still print it in a MessageBox right after the function call might be explained by the fact that the pointer was still good. This didn't hold anymore when I was using the pointer somewhere else in the Window procedure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  2. Global Variables in C++?
    By DeX in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2005, 08:43 AM
  3. Replies: 1
    Last Post: 09-05-2004, 07:42 PM
  4. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  5. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21