CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2011
    Posts
    60

    invalidate()/updatewindow() debug vs. release

    Hello,

    Thank you for taking the time from your busy day to help me out.

    I'm running the invalidate()/Updatewindow() combo to refresh a dialog box. This crashes the program in debug mode but runs fine in release mode.

    void Status::repaintstatus()
    {
    Invalidate();
    UpdateWindow();
    }

    I've read a few debug vs release mode articles but could not find a solution to this error. Again, thank you very much for your time.

    Greg

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: invalidate()/updatewindow() debug vs. release

    Not enough info. There's nothing in that code that should cause a crash. Use the debugger to examine your locals, particularly the this pointer. Something's getting messed up upstream.

  3. #3
    Join Date
    Mar 2011
    Posts
    60

    Re: invalidate()/updatewindow() debug vs. release

    Oh wow. The this pointer was getting corrupted as it turns out I was calling these functions while the dialog box wasn't even enabled!!! I don't know what I was thinking and I don't why release mode wouldn't catch this...haha. Thank you very much. The dialog box these functions are being used on is a popup dialog box. Do you know of a way to check when the dialog box is enabled? I found the IsWindowEnabled() function but I'm guessing I somehow need to make a pointer to the CWnd* in the constructor? Any help with that would be much appreciated. Again thank you very much.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by Gregorina View Post
    Oh wow. The this pointer was getting corrupted as it turns out I was calling these functions while the dialog box wasn't even enabled!!! I don't know what I was thinking and I don't why release mode wouldn't catch this...haha. Thank you very much. The dialog box these functions are being used on is a popup dialog box. Do you know of a way to check when the dialog box is enabled? I found the IsWindowEnabled() function but I'm guessing I somehow need to make a pointer to the CWnd* in the constructor? Any help with that would be much appreciated. Again thank you very much.
    IsWindowEnabled() is a member of CWnd. You can call it anywhere inside your dialog, or through any instance of a CWnd derived class. Windows still paint whether they're enabled or not, so that's probably not what you're looking for. Do you mean you had a CDialog object that didn't have an associated window yet? If so, use IsWindow().

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by Gregorina View Post
    Do you know of a way to check when the dialog box is enabled? I found the IsWindowEnabled() function but I'm guessing I somehow need to make a pointer to the CWnd* in the constructor? Any help with that would be much appreciated. Again thank you very much.
    If you call the function outside a scope where you are sure the window exists, you can use
    Code:
    void Status::repaintstatus()
    {
    	if (IsWindow(GetSafeHwnd()) {
    		Invalidate();
    		UpdateWindow();
    	}
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Mar 2011
    Posts
    60

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by GCDEF View Post
    IsWindowEnabled() is a member of CWnd. You can call it anywhere inside your dialog, or through any instance of a CWnd derived class. Windows still paint whether they're enabled or not, so that's probably not what you're looking for. Do you mean you had a CDialog object that didn't have an associated window yet? If so, use IsWindow().
    Yes there was no window for the Status CDialog. I am using IsWindow(GetSafeHwnd()) as Drmmr suggested, but even though the Status dialog has a window, IsWindow(GetSafeHwnd()) still returns 0 and the if statement isn't entered. I'm not really sure when the dialog box will have a window, that's why I'm checking. Maybe it would be easier to set a flag in the oninitdialog? Then I would have to reset the flag when the dialog box is gone, what virtual function would I use for that?

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by Gregorina View Post
    Yes there was no window for the Status CDialog. I am using IsWindow(GetSafeHwnd()) as Drmmr suggested, but even though the Status dialog has a window, IsWindow(GetSafeHwnd()) still returns 0 and the if statement isn't entered. I'm not really sure when the dialog box will have a window, that's why I'm checking. Maybe it would be easier to set a flag in the oninitdialog? Then I would have to reset the flag when the dialog box is gone, what virtual function would I use for that?
    Unless you override the default behavior, the window is created in OnInitDialog, and destroyed in EndDialog. Are you calling IsWindow inside or outside the dialog?

  8. #8
    Join Date
    Mar 2011
    Posts
    60

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by GCDEF View Post
    Unless you override the default behavior, the window is created in OnInitDialog, and destroyed in EndDialog. Are you calling IsWindow inside or outside the dialog?
    I'm calling it inside the status dialog class, but the dialog may not necessarily be created yet from the parent dialog. Actually I added a flag to set after the status dialog box is created:

    Code:
    void Status::repaintstatus()
    {		
    	if(check)
    	{
    		Invalidate();
    		UpdateWindow();
    	}
    }
    But even though the dialog box is created Invalidate() still crashes the program. I know the this pointer is getting corrupted somewhere, but I can't find where. This code was given to me at work and was written by someone else and it has threads and is about 20000 lines of code. It's hard to step through. Is there a way in the debugger to add a variable to the watch and then skip ahead to where the value of that variable changes?
    Last edited by Gregorina; December 11th, 2013 at 05:37 PM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: invalidate()/updatewindow() debug vs. release

    Just set a breakpoint, right click on the red dot, select condition, then select when changed.

  10. #10
    Join Date
    Mar 2011
    Posts
    60

    Re: invalidate()/updatewindow() debug vs. release

    It gets corrupted on the Status status; line.

    Code:
    Status status;
    if(condition)
    {
    status.repaintstatus();			
    }
    Code:
    void Status::repaintstatus()
    {		
    	if(check)
    	{
    		Invalidate();
    		UpdateWindow();
    	}
    }

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by Gregorina View Post
    It gets corrupted on the Status status; line.

    Code:
    Status status;
    if(condition)
    {
    status.repaintstatus();			
    }
    Code:
    void Status::repaintstatus()
    {		
    	if(check)
    	{
    		Invalidate();
    		UpdateWindow();
    	}
    }
    What gets corrupted? The this pointer for status class instance? If this is right, then the constructor for Status class is a likely candidate for the corruption problem.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: invalidate()/updatewindow() debug vs. release

    Quote Originally Posted by Gregorina View Post
    It gets corrupted on the Status status; line.

    Code:
    Status status;
    if(condition)
    {
    status.repaintstatus();			
    }
    What is your Status? Is it a dialog? It is not enough to construct dialog object, you need to create its window.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured