We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Current object is deleted but continues executing accessing invalid memory

Brent-Campbell
on
Medium Priority
252 Views
Last Modified: 2010-04-17
Hi,

Looking at my simplified code below (nothing to do with MFC), basically what I believe is happening is that this method is entered, the messagehandler in the code is called on another object which in turn this object get's deleted (when needed).  Obviously if this object does get deleted the problem now is that this method continues executing but m_bComplete no longer exists and therefore causes an access violation error! Is there anyway to check whether this object is still valid at this point? or is there a method to get around this?

Many thanks in advance!


void Button::OnLeftMouseButtonDown(int x, int y, WPARAM status)
{
   if (m_pObjectToReceiveMessages)
   {
      // Send a 'Left Mouse Button Down Single Click' message to the respective
      //  message handler
      m_pObjectToReceiveMessages->MessageHandler(GetName(), LEFT_MOUSE_BUTTON_DOWN_SINGLE_CLICK, NULL);
   }

   m_bComplete = true;
}
Comment
Watch Question

First of all you are breaking some rules of the Object Oriented Programming.
The class that created this object is the one that should destroy it.
Second, when you have entered the event handler of ANY object, you shouldn't
try to destroy that object in the current event handler.
Third and the most lovely :) this can be avoided easily.

There are two API functions: SendMessage and PostMessage.

LRESULT SendMessage(
    HWND hWnd,      // handle of destination window
    UINT Msg,      // message to send
    WPARAM wParam,      // first message parameter
    LPARAM lParam       // second message parameter
   );
The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message.


BOOL PostMessage(
    HWND hWnd,      // handle of destination window
    UINT Msg,      // message to post
    WPARAM wParam,      // first message parameter
    LPARAM lParam       // second message parameter
   );
The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message.




Now, SendMessage "does not return until the window procedure
has processed the message" and PostMessage "(posts) a message
in the message queue associated with the thread that created the
specified window and then returns without waiting for the thread
to process the message"

The solution for your code is to use PostMessage, thus, as soon as
your code gets out of that Handler, and your application continues
to loop the main message queue, your object will be destroyed
(because when you call .Destroy you place WM_DESTROY message
in the app queue)

So, use PostMessage(WndHandle, WM_DESTROY, 0, 0) instead of
SendMessage(WndHandle, WM_DESTROY, 0, 0)
in the m_pObjectToReceiveMessages->MessageHandler()

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Sorry for the extremely late reply!

You have some very valid points there.  Unfortunately I can't use PostMessage and SendMessage as the application is written soley in OpenGL so I've now made my own PostMessage routine which now solves the problem.

Many thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.