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

Current object is deleted but continues executing accessing invalid memory

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;
}
0
Brent-Campbell
Asked:
Brent-Campbell
1 Solution
 
nepostojeci_emailCommented:
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()
0
 
Brent-CampbellAuthor 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!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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