Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

the_grip

Multiprocess Question

This topic is 6276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Here's the code i have:

    
//in the view i am working with... this gets filled with values

CArray<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 file

UINT 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!