• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

CFormView class data members getting corrupted

I have a class derived from CFormView which contains instance data such as a pointer to a CStatic which is used to load and display a bitmap and is initialized in OnInitialUpdate(). The problem is, when entering other class member functions, the hWnd of the CStatic* is no longer valid. I have worked around it by using GetDlgItem() every time I want to reference the control. This problem does not occur in CDialog derived classes, just when trying to do the same thing in a CFormView. Is there a special way to treat member data such that it remains stable from member function to member function??
1 Solution
The return value from GetDlgItem() is usually a temporary CWnd object. When it's a temporary, MFC will delete it when MFC is done processing the current message.

By holding onto that pointer, you're going to end up referencing an object that no longer exists.

You have a couple of choices:

1) Add a CStatic member to your CFrameView-derived class with ClassWizard. Call Update() from your OnInitialUpdate().  Use that member
instead of your GetDlgItem() pointer.

2) Get a permanent CWnd object with FromHandlePermanent().  This is what MFC's doing under the covers in step #1.

..B ekiM
captainkirkAuthor Commented:
Thank you, Mike --  u r a genius...

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now