Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - Grep Leaks Memory
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Grep Leaks Memory

 Post Reply Post Reply
Author
Message
danpetitt View Drop Down
Senior Member
Senior Member


Joined: 17 July 2005
Location: United Kingdom
Status: Offline
Points: 109
Post Options Post Options   Thanks (0) Thanks(0)   Quote danpetitt Quote  Post ReplyReply Direct Link To This Post Topic: Grep Leaks Memory
    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.
Back to Top
Oleg View Drop Down
Senior Member
Senior Member


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
danpetitt View Drop Down
Senior Member
Senior Member


Joined: 17 July 2005
Location: United Kingdom
Status: Offline
Points: 109
Post Options Post Options   Thanks (0) Thanks(0)   Quote danpetitt Quote  Post ReplyReply Direct Link To This Post 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 );
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.080 seconds.