PostMessage memory handling

I have a MDI application with a worker thread receiving approximately 3000 messages per minute from a socket. Some of the messages have to be processed by Child Windows if they are active. A message could apply to more than one child window.

If the worker thread post a message to the parent window, when and where can I delete the message created in the worker thread, as the worker thread doesn’t know when the child windows have finished their processing?

PS: I don't need to use SendMessage.
DBOTMAAsked:
Who is Participating?
 
Roshan DavisConnect With a Mentor Commented:
Yes thatz Y I created a new CString in the EnumUpdateChildProc ...please look the EnumUpdateChildProc function. (CString *pNewString = new CString()) ... Creating a new CString for each child, so it will not fail.
0
 
Roshan DavisCommented:
Use PostMessage itslef, but add your string messages to a global array and pass that index thru PostMessage. When the child accepts the message, process, delete from there itself.
0
 
DBOTMAAuthor Commented:
Your message: "delete from there itself", where is the "there"?  Do you mean from the child window, if so, what if other child windows still reading/processing the message? ALSO if the receiving window/process has to delete the messages what will happen with messages still in the  queue if the window close.  
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
Roshan DavisCommented:
If you know the count of child windows, you can use a reference count that will decrement on each child process, delete when that count becomes zero. I donno know how much this suitable for your project, just a guess
0
 
DBOTMAAuthor Commented:
1. I can't wait for all the child windows to close before I delete the messages, the array will be too large. Thus, I have to delete messages once they are served by all the open child windows.

2. This is still unanswered: "ALSO if the receiving window/process has to delete the messages what will happen with messages still in the  queue if the window close." ie it' won't be deleted
0
 
Roshan DavisCommented:
Please give some more idea on ur application

1. is that *message* you specified is a heap allocated memory?
2. Which parent window accepting this messages..? CChildFrame?..if yes, please mention all typ child controls that have this parent.

Rosh :)
0
 
DBOTMAAuthor Commented:
This is a snippet, hope it will make it more clear :

Global Variable
===============

CWnd * gpWnd;

Worker Thread
=============
VOID WorkerThread::SendMessage()
{
                  CString* tmpMsg = new CString();   //I think, This will become an array

                  tmpMsg->Append("....Message read from socket.....");

                  EnumChildWindows(gpWnd->m_hWnd, EnumUpdateChildProc, (LPARAM) &tmpMsg);
}


BOOL CALLBACK EnumUpdateChildProc(HWND hwnd,    LPARAM lParam )
{
      if (!(gpWnd == NULL))
            PostMessage(hwnd, WM_SECURITY_UPDATE, 0, lParam);
      
      return TRUE;
}


MDI
===

BEGIN_MESSAGE_MAP(CMyRecView, CRecordView)
      ON_MESSAGE(WM_SECURITY_UPDATE, OnUpdate)
END_MESSAGE_MAP()


void CMyRecView::OnInitialUpdate()
{
      m_pSet = &GetDocument()->m_WatchlistRecSet;
      CRecordView::OnInitialUpdate();

      gpGridWatchlist = FALSE;

      if (gpWnd== NULL)
            gpWnd= GetParentOwner();
}


LONG CMyRecView::OnUpdate(WPARAM, LPARAM lParam)
{
      
      .....Update grid on child window .....
      
      return 0l;
}
0
 
Roshan DavisCommented:
Another try out

simply define some strings like

#define IDS_MESSAGE_INITIALIZE       "....Initializet....."
#define IDS_MESSAGE_CONNECTED      "....Connected....."
#define IDS_MESSAGE_INITIALIZE      "....Message read from socket....."
#define IDS_MESSAGE_COMPLETED      "....Completed....."


etc

and pass a number in the PostMessage, from that number you can identify which string you needed from that define....So the strings will not be duplicated and no Strings will be allocated in heap for deletion

Good luck
0
 
DBOTMAAuthor Commented:
I'm lost...........(a few more point might make it worth the effort)


Please note the code :

 tmpMsg->Append("....Message read from socket.....");

is more like:

            for(;;){
                  CString* tmpMsg = new CString();  
                  ...
                  ...
                  recv(hSocket, &msgRead, byteRx,0)
                                                ...
                                                ...
                  tmpMsg->Append(msgRead);

                  //PostMessage in EnumUpdateChildProc
                  EnumChildWindows(gpWnd->m_hWnd, EnumUpdateChildProc, (LPARAM) &tmpMsg);
            }

the string "....Message read from socket....." is not what I want to send but the message read from the socket.

Thanks

0
 
Roshan DavisCommented:
What about this

VOID WorkerThread::SendMessage()
{
      CString* tmpMsg = new CString();   //I think, This will become an array

      tmpMsg->Append("....Message read from socket.....");

      EnumChildWindows(gpWnd->m_hWnd, EnumUpdateChildProc, (LPARAM) &tmpMsg);
}


BOOL CALLBACK EnumUpdateChildProc(HWND hwnd,    LPARAM lParam )
{
    if (!(gpWnd == NULL))
      {
            CString *pNewString = new CString();
            CString* tmpMsg = (CString* )lParam;

            pNewString->Append(tmpMsg);

            delete tmpMsg;

         PostMessage(hwnd, WM_SECURITY_UPDATE, 0, (LPARAM)pNewString);
      }
   
     return TRUE;
}


LONG CMyRecView::OnUpdate(WPARAM, LPARAM lParam)
{

      CString* tmpMsg = (CString* )lParam;
   
     .....Update grid on child window .....

       delete tmpMsg;
   
     return 0l;
}
0
 
DBOTMAAuthor Commented:
More Points... :-)

Ok, now we are were we started.

How I understand MDI is that  the function CMyRecView::OnUpdate(WPARAM, LPARAM lParam)  will apply to each instanciation of a Child Window. For every new child window this function OnUpdate will execute if the worker thread sends a message. ie more than one Child Window will receive the message and each one will delete the tmpMsg which I think will cause failure.

   
0
 
SteHCommented:
What about posting the message to a central document like class. This class then keeps track of all messages received. All open views need to update their view using the data stored in this class. That way the worker thread is sending the message to only one recipient which knows it can delete the CString object. And all views can read the data. Since now the document class and the views are in the same thread a synchronisation should not be necessary.
0
All Courses

From novice to tech pro — start learning today.