Thread: Basic Controller Question

  1. #1
    Registered User
    Join Date
    Sep 2007

    Basic Controller Question


    I've been programming C++ in my spare time for a year or two, but am new to windows programming. I've been looking at a tutorial about programming stuff for windows, which is available here:

    Windows API Tutorial: Generic

    I have a question which I think is pretty simple. I doubt you guys would even need to download the example to understand the problem. Say if you add the following to the top of control.cpp:

    #include <time.h>
    #include <ctime>

    Then, at the top of your MainWndProc function,

    time_t seconds;
    int timenow = clock() / CLOCKS_PER_SEC;

    And, after pCtrl is declared in MainWndProc,

    if (timenow > 1)
    pCtrl->Paint ();

    And finally, you delete pCtrl->Paint (); from beneath case WM_PAINT: in the message loop.

    What happens is, instead of it displaying a line/ some text when the program starts, it waits a couple of seconds and then displays it. And then, when you close the program, it crashes, presumably because pCtrl has been deleted. OK, so if that's the case, is there a way to check if pCtrl is in existence when I do this? By the way the reason I check if timenow is over 1 is because it doesn't work otherwise.

    if (timenow > 1)
    pCtrl->Paint ();


  2. #2
    Join Date
    Oct 2007
    Inside my computer
    Actually, you must delete pCtrl, not pCtrl->Paint(). Or is that a typo?
    And the whole timenow > 1 is your culprit that delays the rendering of text.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Oh yeah, oops, I meant delete pCtrl. The problem is that

    if (timenow > 1)
    pCtrl->Paint ();

    always gets run after it's been deleted.

    I actually do want it to delay the display of the text, sorry if I didn't explain that properly.

    Ultimately I'm trying to make the program automatically paint stuff on the screen at different times. I've done more complicated stuff like add a few int variables into the controller class, and had them change when I print out a line (at positions specified by them), so that you can end up with lines being drawn all over the screen and stuff.

    But the thing is, whenever I do any of that, every time the program ends, it crashes, and I'm trying to figure out how to stop that happening.

    Thanks for your reply.
    Last edited by bengreenwood; 07-19-2009 at 01:35 PM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    near a computer
    Quote Originally Posted by bengreenwood View Post
    And finally, you delete pCtrl->Paint (); from beneath case WM_PAINT: in the message loop.
    If you delete the pCtrl object in a WM_PAINT msg then the next msg generated by windows will crash your app.

    Only delete this object in response to the WM_DESTROY msg.

    When the window is created a instance of the Controller object is allocated. A pointer to this object is stored under the window, in a location called 'user data'.

    Each time a msg is sent through the callback, a pointer to the Controller object is retrieved from the windows 'user data' using GetWindowLong and the msg's HWND.

    If you delete the pCtlr object, but do not 'clear' the HWNDs user data, the next msg will get an invalid (dangling) pointer passed to it (by Controller * pCtrl = WinGetLong<Controller *> (hwnd))

    You will then call a method on an object instance you have deleted (== crash)

    Quote Originally Posted by bengreenwood
    Ultimately I'm trying to make the program automatically paint stuff on the screen at different times.
    Look at 'timers'.

    You create a timer
    Process the WM_TIMER msgs (it fires every time interval, not just once)
    Kill the timer when finished with it (ie WM_DESTROY or once fired)

    //pure WIN32 API, you will need to modify for that wrapper
    //global define of timer ID number (resouce.h)
    #define IDT_TEXT_REFRESH         101 
    //create a timer to fire each second called IDT_TEXT_REFRESH
    //that will send WM_TIMER msgs to the main callback (wndproc for the HWND we give it)
    SetTimer(hWnd, IDT_TEXT_REFRESH, 1000, NULL); //times are in ms (this is not high resolution, >100 ms)
    //process the timer msgs
    //check which timer has fired
    if(wParam == IDT_TEXT_REFRESH)
         //refresh the text etc
    //note kill the timer in WM_TIMER if you only want it to fire once
    //end the timer 
    KillTimer(hWnd, IDT_TEXT_REFRESH);
    Last edited by novacain; 07-24-2009 at 01:19 AM.
    "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

  5. #5
    Registered User
    Join Date
    Sep 2007
    Thanks for that mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM