Print Page | Close Window

Grep Leaks Memory

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=5045
Printed Date: 07 July 2025 at 3:12am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Grep Leaks Memory
Posted By: danpetitt
Subject: Grep Leaks Memory
Date Posted: 14 September 2006 at 2:23am
I have the same problem in my own app, but with the Grep sample if you start a search and exit the application while the search is executing the ExitInstance doesnt get called and the objects leak.
 
What's the solution as I have spent days and rewritten this part of my app 6 times and I still get the same problem. I have tried waiting in MainFrame::OnClose until thread finishes but it never does, argh!
 
Which is why I tried the Grep sample knowing that uses CWinThread as well and it suffers the same.
 
To give the thread time to cleanup I added this to MainFrame::OnClose
 if( m_pSearchThread )
 {
  m_pSearchThread->m_bCancel = TRUE;
  while( m_pSearchThread )
  {
   Sleep( 1000 );
  }
 }
 
But the thread gets ExitInstance called but the finished message never arrives so this loop never exits.
 
Any ideas on why and the solution please?
 
Thanks.



Replies:
Posted By: Oleg
Date Posted: 14 September 2006 at 3:56am
Hello,
Thank you for this note.
 
Code you need is:
 
 if (m_pSearchThread)
 {
  m_pSearchThread->m_bCancel = TRUE;
  WaitForSingleObject(m_pSearchThread->m_hThread, INFINITE);
  m_pSearchThread = NULL;
 }


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: danpetitt
Date Posted: 14 September 2006 at 6:23am

Thanks a lot Oleg, I did notice that I had to use a member variable for the thread handle and use DuplicateHandle so its not closed before I can investigate it.

As it uses messages to indicate to the primary thread that it has finished, I had to add a message pump in the wait, so I ended up with (something like):
 
HANDLE m_myDuplicateThreadHandle;
VERIFY( ::DuplicateHandle( GetCurrentProcess(), m_pSearchThread->m_hThread, GetCurrentProcess(), &m_myDuplicateThreadHandle, 0, FALSE, DUPLICATE_SAME_ACCESS ) );

while( ::WaitForSingleObject( m_myDuplicateThreadHandle, 10 ) == WAIT_TIMEOUT )
{
 MSG msg;
 while( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
 {
 if( !AfxGetApp()->PumpMessage() )
 {
  ::PostQuitMessage( 0 );
  break;
 }
 }
 LONG lIdle = 0;
 while( AfxGetApp()->OnIdle( lIdle++ ) );
 Sleep( 0 );
}
CloseHandle( m_myDuplicateThreadHandle );



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net