Solved

PostMessage memory handling

Posted on 2003-12-02
12
380 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:DBOTMA
  • 6
  • 5
12 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864076
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
 

Author Comment

by:DBOTMA
ID: 9864189
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864248
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
 

Author Comment

by:DBOTMA
ID: 9864463
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864529
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
 

Author Comment

by:DBOTMA
ID: 9864755
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9864884
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
 

Author Comment

by:DBOTMA
ID: 9865049
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9865097
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
 

Author Comment

by:DBOTMA
ID: 9865207
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
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 250 total points
ID: 9865234
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
 
LVL 13

Expert Comment

by:SteH
ID: 9865301
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 81
Doc'in system (example?) BA 7 78
twoTwo  challenge 35 86
noX challenge 17 76
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now