Thread: Problem TextOut'ing vector<string>

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    New York

    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:

    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;
            case WM_CREATE:
            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);
            case WM_DESTROY:
                ::PostQuitMessage( 0 );
        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
    The edge of the known universe
    > 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.

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, 04: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, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM