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??
LVL 4
captainkirkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikeblasCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
captainkirkAuthor Commented:
Thank you, Mike --  u r a genius...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.