Problem TextOut'ing vector<string>

This is a discussion on Problem TextOut'ing vector<string> within the Windows Programming forums, part of the Platform Specific Boards category; Notes: compiles fine, VC++, Windows XP, i'm a noob. Hello, I'm trying to learn C++ vectors in a windows program, ...

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Problem TextOut'ing vector<string>

    Notes: compiles fine, VC++, Windows XP, i'm a noob.

    Hello, I'm trying to learn C++ vectors in a windows program, and I think that I've pin-pointed that my problem should live in the Windows programming forum. I have a vector<string> that sort of "logs events" that are push_back'ed onto it as they occur in their respective messages. So I messily hacked this stuff together, my WndProc:

    Code:
    LRESULT CALLBACK WindowProcedure
        (HWND hWindow, UINT message, WPARAM wParam, LPARAM lParam)
    {    
        HDC                hdc;
        int                j = 20;
        RECT               rcRect;
        PAINTSTRUCT        ps; 
    
        std::vector<std::string> rawr;
        std::vector<std::string>::const_iterator i;
    
    
    
        rawr.push_back("blah");
        rawr.push_back("rawr");
        rawr.push_back("test");
        switch(message)
        {
            case WM_CREATE:
                rawr.push_back("Created");
                break;
            case SOMEMOREEVENTSTOHANDLE:
                pushbacksomemorestring();
                break;
    
    
    
            case WM_PAINT:
                hdc = ::BeginPaint(hWindow, &ps);
                RECT rcRect;
                ::GetClientRect(hWindow, &rcRect);
                for(i = rawr.begin(); i != rawr.end(); ++i, j += 20)
                {
                    ::TextOut(hdc, rcRect.left, rcRect.bottom - j, (*i).c_str(), (*i).length());
                }
                ::EndPaint(hWindow, &ps);
                break;
    
    
            case WM_DESTROY:
                ::PostQuitMessage( 0 );
                break;
        }    
        return ::DefWindowProc
            (hWindow, message, wParam, lParam);
    }
    What happens: blah, test, and rawr, the ones outside the message handler switchy(), are printed to the screen just fine, but when I try to push back other messages like "Window created" "Tonto is a n00b" "etc..." then they are never drawn. I really don't know the problem here, can a new pair of eyes give me a hand with this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > rawr.push_back("Created");
    This only exists whilst the message is WM_CREATE. When the function exits, all of rawr is deleted (it's just another local variable going out of scope).

    So when the function is next called with message WM_PAINT, the vector is recreated with the 3 strings you see.

    Make the vector global, static or a member of some class (anything which persists longer than a local variable basically).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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