Multithreaded server application - increase in application memory usage

I have created a multithreaded server application in Windows service. In the ServiceMain() function, a listening socket is created to listen for incoming client request. Upon acceptiong a new client request, a new thread will be created to handle that request. The client request is handled by the CMyThread class (parent class is CWinThread), but for debugging purpose, there is no specific implementation in the class yet. I observed that, the service application memory usage keeps on increasing against time. Each new client request will result in about 4K increase in application memory usage after the CMyThread servicing thread terminates. I am not quite sure how the AfxBeginThread() allocates memory in heap and how the CWinThread releases all used resources back to heap. During source debugging, I observed that the CMyThread object was deleted after the ExitInstance() was called.

The service application works well when no thread is created upon receiving new client request. Appreciate if someone can give me a hand on this. Thanks.

Below are snippets of the codes:

// service main function
void CMyService::ServiceMain(DWORD /*dwArgc*/, LPTSTR* /*lpszArgv*/)
{
:
:

CAsyncSocket socListen;
if (!socListen.Create(8080))
{
QuitService(strMsg);
return;
}
if (!socListen.Listen(200))
{
QuitService(strMsg);
return;
}

:
:

CSocket soc;
CMyThread* pThread;
while(!m_bStop)
{
if (socListen.Accept(soc))
{
pThread = (CMyThread*)AfxBeginThread(RUNTIME_CLASS(CMyThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);

if (!pThread)
{
soc.Close();
strMsg.Format(_T("Failed to create serving thread. Error: %s."), GetLastErrorText(GetLastError()));
AfxGetService()->m_EventLogSource.Report(EVENTLOG_ERROR_TYPE, CS_MSG_SERVICE_ERROR, strMsg);
return;
}

// Pass the socket to the thread by passing the socket handle.
pThread->m_hSocket = soc.Detach();

// start the thread.
pThread->ResumeThread();

}
Sleep(1000);
}

:
:
}

// CMyThread class: MyThread.h
class CMyThread : public CWinThread
{
DECLARE_DYNCREATE(CMyThread)

protected:
CMyThread(); // protected constructor used by dynamic creation
virtual ~CMyThread();

public:
// Used to pass the socket handle from the main thread to this thread.
SOCKET m_hSocket;

// CSocket derived class that handles the connection.
CSock m_socket;

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyThread)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual int Run();
//}}AFX_VIRTUAL
};

// CMyThread class: MyThread.cpp
#include "MyThread.h"

IMPLEMENT_DYNCREATE(CMyThread, CWinThread)

CMyThread::CMyThread()
{
m_bAutoDelete = TRUE;
}

CMyThread::~CMyThread()
{
}

BOOL CMyThread::InitInstance()
{
:
:

// Attach the socket handle to a CSocket object.
m_socket.Attach(m_hSocket);
m_socket.m_pThread = this;

:
:

return TRUE;
}

int CMyThread::ExitInstance()
{
m_socket.ShutDown();
m_socket.Close();

:
:

return CWinThread::ExitInstance();
}

int CMyThread::Run()
{
// TODO: Add your specialized code here and/or call the base class
PostQuitMessage(0);
return CWinThread::Run();
}
[3722 byte] By [ffkong] at [2007-11-20 1:19:56]
# 1 Re: Multithreaded server application - increase in application memory usage
What method are you using to observe memory usage?

Viggy
MrViggy at 2007-11-9 13:59:06 >
# 2 Re: Multithreaded server application - increase in application memory usage
By looking at the memory usage of the process in the task manager.
ffkong at 2007-11-9 14:00:08 >
# 3 Re: Multithreaded server application - increase in application memory usage
Task Manager is a very crude tool for measuring memory usage. There might not be anything wrong at all.

On the other hand, another use has reported a memory leak, confirmed by Microsoft, when using CAsyncSocket in a GUI app that also has XP visual styles (themes). See "CSocket Consuming Memory Uncontrollably" at http://www.dev-archive.com/forum/showthread.php?t=353944 and the solution (which is to disable themes) at "CAsynCSocket Memory Leak Fix" at http://www.dev-archive.com/forum/showthread.php?t=370761

This might not be your problem, but it's worth knowing.

Mike
MikeAThon at 2007-11-9 14:01:07 >