Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

WaitForSingleObject blocked whereas a thread has returned

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JsCharly - 12 Sep 2007 07:46 GMT
Hello,

When closing my multithread application, before stopping the main thread I
wait, in this main thread, for a secondary thread to exit. I use for it the
function WaitForSingleObject  which never end.

When debugging I can check that this secondary thread is returning zero and
exiting but by using spy++ I can see that this thread is still existing.

What can prevent this thread to terminate whereas this thread as returned
zero ?

Code sample :

HANDLE hThreadInterrogationPorte;

class CCNR_FonctionsDeBaseCN
{

(...)

static DWORD WINAPI ThreadInterrogationPorte(LPVOID lpParam);

(...)

}

DWORD WINAPI CCNR_FonctionsDeBaseCN::ThreadInterrogationPorte(LPVOID lpParam)
{
   (...)
   return 0;
}

CCNR_FonctionsDeBaseCN::CCNR_FonctionsDeBaseCN(CString PathOfCurrentDir)
{

(...)   

DWORD ThreadId;

m_ParamCycleInterrogationPorte.hThreadInterrogationPorte =
CreateThread(
NULL,                                           //LPSECURITY_ATTRIBUTES
lpThreadAttributes,
0,                                      //SIZE_T dwStackSize,
&ThreadInterrogationPorte,            //LPTHREAD_START_ROUTINE lpStartAddress,
&m_ParamCycleInterrogationPorte, //LPVOID lpParameter,
0,               //Run immediately DWORD dwCreationFlags,
&ThreadId                                       //LPDWORD lpThreadId
);

(...)

}

CCNR_FonctionsDeBaseCN::~CCNR_FonctionsDeBaseCN()
{

 (...)

 if(m_ParamCycleInterrogationPorte.hThreadInterrogationPorte != NULL)
        WaitForSingleObject(m_ParamCycleInterrogationPorte.hThreadInterrogationPorte,INFINITE);

  (...)   
}

Best regards
   
JsCHarly
Carl Daniel [VC++ MVP] - 13 Sep 2007 03:16 GMT
> Hello,
>
[quoted text clipped - 12 lines]
> returned
> zero ?

The thread object will continue to exist until all handles to the thread
have been closed.  The thread can't run anymore in this state, but it does
still exist.

-cd
Jochen Kalmbach [MVP] - 13 Sep 2007 06:05 GMT
Hi Carl!

>> When debugging I can check that this secondary thread is returning
>> zero and
[quoted text clipped - 8 lines]
> have been closed.  The thread can't run anymore in this state, but it does
> still exist.

But the thread-handle will be signaled if the the thread has terminated.

But it seems that for some reason the thread has not terminated.

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

Carl Daniel [VC++ MVP] - 13 Sep 2007 06:24 GMT
> Hi Carl!
>
[quoted text clipped - 14 lines]
> terminated.
> But it seems that for some reason the thread has not terminated.

If it has returned from it's thread main function but hasn't terminated, I'd
guess it's somehow blocked in the CRT shutdown code.  I'd be suspicious of a
corrupt heap state of some sort.  I'd also try inserting a call to
TerminateThread to forcibly kill the thread & see what happens - that's not
the solution, but it might be illuminating.

-cd
JsCharly - 02 Oct 2007 08:08 GMT
Hello,

I've tried to use TerminateThread and now the terminated thread is signaled.

You said that the terminated thread may be non signaled because of a corrupt
heap state of some sort.

In debug mode Visual studio detect no leak of memory or other problem.
What can I do to detect the cause of this problem ?

JsCHarly

> > Hi Carl!
> >
[quoted text clipped - 22 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 02 Oct 2007 17:06 GMT
> Hello,
>
[quoted text clipped - 6 lines]
> In debug mode Visual studio detect no leak of memory or other problem.
> What can I do to detect the cause of this problem ?

That's consistent with the thread being blocked.  The CRT has some shared
state, and uses windows synchronization mechanisms to access that state (a
Mutex, if I recall correctly).  I suspect that the thread is blocked trying
to get access to that shared state, but due to data corruption elsewhere,
the thread that owns the shared state is either also blocked or has
terminated improperly.

My first angle of attack would be to attach a debugger and attempt to
examine the stack of the blocked thread to see if you can work out where
exactly it's blocked.

If you have access to such a tool, you might try running the code through a
static analyzer like Purify or the /analyze option of VC++ 2005 Team Edition
to see if they can catch anything. You might also try running the code under
a dynamic analyzer like Bounds Checker to see if they catch anything.

-cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.