I'm encountering a memory problem in a threaded application that uses DAO.

I do know that the DAO technology is deprecated and that it is not thread-safe. I am making sure that only one thread accesses the database at a time using the EnterCriticalSection / LeaveCriticalSection approach, by surrounding the call to the database-handling method, NOT by surrounding the invocations of the threads. That is, the threads may be processing asynchronously by the access to the database is assured to be "one-at-a time".

At the beiginning of the processing of each thread I call AfxDaoInit(). The program runs just fine, doing all the threading and database processing perfectly.

BUT ! I also keep an eye on the memory usage of the program. I have the
Windows Task Manager window open and watch the Performance tab which shows memory usage. As my program handles all the therads, and invokes that AfxDaoInit time and time again, the memory usage simply explodes, eventually consuming all available memory. If I then stop the portion of the program that's doing the threads, i.e. I don't call any more threads and all previously active threads have finished their work, then slowly, VERY SLOWLY, while my program is not doing anything else, the memory gets reclaimed. I can watch the memory usage on the Windows Task Manager slowly decrease the PF usage and slowly increase the Available Physical Memory.

So, first question: Is there any way to force the application to reclaim
memory while it's running, ideally at the end of each thread's processing?


I tried some things...

I tried calling AfxDaoTerm at the end of each thread. That caused exceptions to be thrown, because the threads are asynchronous and there's no guarantee that there's matching invications of AfxDaoInit and AfxDaoTerm.

I tried setting up an invocation counter, like:

Code:
static int nDaoInitializationCount = 0;


BOOL CHostTransactionHandlerThread::InitInstance()
{
	if (0 == nDaoInitializationCount)
	{
		AfxDaoInit();
		++nDaoInitializationCount;
	}


	do the database processing


	--nDaoInitializationCount;
	if (0 == nDaoInitializationCount)
	{
		AfxDaoTerm();
	}

	return(FALSE);
}

so that AfxDaoTerm would only get called when no thread needed it.
That still resulted in exceptions, the problem being that each and every
thread absolutely MUST cann AfxDaoInit.


So, I'm at a loss as to how to proceed.

It NEEDS AfxDaoInit at the BEGINNING of each thread.
It HATES AfxDaoTerm at the END of each thread.
It consumes enormous amounts of memory while running.
But Windows seems to reclaim the memory if I just let the program run
after the threading process has completed.

I'm open to suggestions (other than "Don't use DAO", since I have to for this app.).


- Roger