Subclassing a listbox and the speed of the postal service

This is a discussion on Subclassing a listbox and the speed of the postal service within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm currently subclassing a listbox, with a view to turning it into a property viewer as featured in the ...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262

    Angry Subclassing a listbox and the speed of the postal service

    Hi,

    I'm currently subclassing a listbox, with a view to turning it into a property viewer as featured in the Visual Basic IDE. The thing is, I've added a new message to the WindowProc which adds a property to the window and at this early stage that effectively means some basic formatting and using LB_ADDSTRING twice. It looks something like this:-
    Code:
    case PVM_INSERTITEM:
    {
    	PVITEM *pvi = (PVITEM *)lParam;
    	char szTemp[32];
    	long ret;
    
    	ret = SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)pvi->pszText);
    
    	// I do some formatting stuff here (tons of switch(...) :p)
    
    	ret = SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)szTemp);
    	return TRUE;
    }
    The property's name is immediately added to the listbox (Because this window is a subclassed listbox, I'm sending it to itself). After the data is formatted into the szTemp string it's also added. However, while the first LB_ADDSTRING message seems to work properly (The name is visible in the listbox), the second one, while a valid string is passed to it, doesn't (Lovely big row of "" ).

    What is strange is that ret, which is supposed to give the index of the new string, doesn't appear to change between the two commands. Replacing "szTemp" with a string constant (such as "0") works, so I think the memory the string's occupying is being freed before the call is finished, some sort of reentrant problem. Help?
    Last edited by SMurf; 09-13-2002 at 03:06 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Difficult to tell from what you've posted other than what you have suggested yourself:

    >>// I do some formatting stuff here (tons of switch(... ) )<<

    If the 'switch' statement you referred to is too large then just zip it up and attach it.

    It seems like one of these problems you will probably have solved by the time you read this.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    But all that happens in the switch statement is that the data type of the property is determined and the appropriate format specifiers in sprintf used. Like I said, a valid string is produced from it, and used in the LB_ADDSTRING message, but when the string is displayed by the listbox it's garbled.

    I'm thinking a "static char *szTemp[32];" might work, but there might be other implications with static variables, so I dunno

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Don't have a clue based on what you've posted, but here's some things to think about:

    Have you tried filling the string variable with an explicit, dummy value and see what happens when you add that? (I note you have already tried "0" as a literal.)

    What is your subclass listbox procedure doing with the LB_ADDSTRING?

    Are you handling the listbox's memory allocation or are you letting the system handle it? (if the system is dealing with it the string should be copied).

    Have you tested the mechanism against a) a normal listbox and b) a simplified subclassed listbox?

    Try outputting the formatted string to a file or MessageBox? Although there are no guarantees that it may identify a particular problem, you should be aware that there are sometimes differences between debug and 'release' builds that can manifest themselves in different ways.

    Sorry I can't be more helpful, but good luck anyway.

Popular pages Recent additions subscribe to a feed

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