Multiprocess Question

Started by
4 comments, last by the_grip 22 years, 7 months ago
Question: i have a small program i built to ping a range of ip addresses. The psuedo code is like this: (Main process of program) ...handle user input... ...pass data as a CArray to a class to start the ping thread... ...ping thread class activates the ping thread and passes data to the ThreadFunc... ...while not blocked, the ThreadFunc passes in addresses to ping from the CArray one at a time, checking for blocking in between... ...the Ping class posts a message to the main thread's hwnd to post reply messages to the user via a LPRESULT function defined in stdafx.h... ...this stdafx.h function is called AllocString, and makes a call to create a new char*... Here is my dilemna - every so often there is a small memory leak. i'm guessing it is from the new char* in stdafx.h caused by the ping thread ending after the main thread. Each of these new char*'s are deleted after their use in the main thread. i was thinking of creating and using a class in stdafx.h to kind of act as a "garbage collector" for the char* that are created that, when destroyed, deletes each of the char*'s that the program created. Can i be sure that this will be called after the ping thread is released? is there a better solution? Thanks!!! "You call him Dr. Grip, doll!" Edited by - the_grip on September 8, 2001 11:16:18 PM
"You call him Dr. Grip, doll!"
Advertisement
Why does you "ping" thread exit after the main thread at all? Shouldn''t you have the main thread stop all it''s child threads as part of it''s own cleaning up code?

codeka.com - Just click it.
Well, i''ve been trying to get it to do that... it doesn''t seem to work though...

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
Here's the code i have:

    //in the view i am working with... this gets filled with valuesCArray<CString, CString> casIPAddresses;//the pingthread class .h file...class CPingThread{public:	CPingThread();	~CPingThread();	void StartPing(UINT nNumRetries, CArray<CString, CString> &casHostAddresses, HWND hHWnd);	void PingEnd();private:	static UINT __stdcall ThreadProc(void* pParam);	HANDLE m_hThread, m_hPauseEvent, m_hSignalEvent;	HWND m_parentHWND;	UINT m_nNumRetries, m_dwID;	CArray<CString, CString> m_casIPAddresses;};//the pingthread class .cpp fileUINT CPingThread::ThreadProc(void* pParam){	CPingThread* pThis = reinterpret_cast<CPingThread*>(pParam);		while (1)	{		HANDLE hObjects[3];		hObjects[0] = pThis->m_hPauseEvent;		hObjects[1] = pThis->m_hSignalEvent;		DWORD dwWait = WaitForMultipleObjects(2,hObjects,FALSE,500);		if (dwWait == WAIT_OBJECT_0)			break;		if (dwWait == WAIT_OBJECT_0 + 1||dwWait == WAIT_TIMEOUT)		{			if(pThis->m_casIPAddresses.GetSize()!=0)			{				CPing ping;				if(pThis->m_casIPAddresses.GetSize()>0) 				     ping.Ping(pThis->m_nNumRetries, pThis->m_casIPAddresses.GetAt(0), pThis->m_parentHWND);				if(pThis->m_casIPAddresses.GetSize()>0) 				     pThis->m_casIPAddresses.RemoveAt(0);			}		}			}	return 0;}CPingThread::CPingThread(){	m_dwID = 0;	m_hPauseEvent =   CreateEvent(NULL,TRUE,FALSE,NULL);	m_hSignalEvent = CreateEvent(NULL,FALSE,FALSE,NULL);		m_hThread = (HANDLE) _beginthreadex(NULL,// Security					    0,// Stack size - use default                                            ThreadProc,// Thread fn entry point					    (void*)this, 0,// Init flag					    &m_dwID);// Thread address}void CPingThread::StartPing(UINT nNumRetries, CArray<CString, CString> &casHostAddresses, HWND hHWnd){	m_nNumRetries = nNumRetries;	m_casIPAddresses.RemoveAll();	m_casIPAddresses.Append(casHostAddresses);	//m_sCurrentPing = sHostAddress;	m_parentHWND = hHWnd;//	::WaitForSingleObject(m_hThread, nNumRetries*1000);	SetEvent(m_hSignalEvent);}CPingThread::~CPingThread(){	PingEnd();}void CPingThread::PingEnd(){	m_casIPAddresses.RemoveAll();	SetEvent(m_hPauseEvent);	WaitForSingleObject(m_hThread,INFINITE);}     


Any help or ways to make this better is appreciated...


"You call him Dr. Grip, doll!"

Edited by - the_grip on September 9, 2001 2:36:01 AM

Edited by - the_grip on September 9, 2001 2:36:40 AM
"You call him Dr. Grip, doll!"
That looks like it would work. I assume you are calling CPingThread::EndPing() before you do your final cleanup in the main thread, right?

Don''t forget to call _endthreadex() when you''re finished with the thread (say after the call to WaitForSingleObject( m_hThread ))

codeka.com - Just click it.
Thanks for the help... i did everything except _endthreadex() because i thought that was an implicit call in this case. Anyways, i''ll give it a shot. Thanks for the help!

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"

This topic is closed to new replies.

Advertisement