🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8737

Re: Thread WaitFor() ....waits forever

#8737
From: Remy Lebeau (TeamB)
Date: Wed, 22 Sep 2004 05:51
59 lines
3548 bytes
"Daniel Stevenson" <d.stevenson@ocreage.com.au> wrote in message
news:415108cd$1@newsgroups.borland.com...

> I've created a fairly simple thread, but when it comes to
> closing down the thread it won't terminate

You are most likely deadlocking your code.  Synchronize() requires an active
message queue in the main thread.  Your TerminateAndWaitFor() method is not
allowing the main thread to process new messages at all.

> or it causes an Access Violation.

You are mis-using the thread pointer.  See below.

>     delete MyForm1;
>     MyForm1 = NULL;

You should never call 'delete' on a thread that has its FreeOnTerminate
property set to true.  When set to true, the thread object automatically
frees its memory when the underlying thread is fully terminated.  By using
'delete' as well, you are causing a double-deletion to occur on the memory,
which results in undefined behavior.  An AV is just a simple result, but
anything could happen, even as much as a total crash.

If you intend on using 'delete' manually then you should not be using
FreeOnTerminate at all.  In this particular case, FreeOnTerminate is not a
good choice to use because you are accessing the thread object after you
have created it.  By using FreeOnTerminate, the thread object could go away
from memory at any time whatsoever, including while you are trying to access
it.  FreeOnTerminate is only good to use when you want to start a thread and
then forget about it altogether and let it manage itself completely.  That
is not the case in the code you have shown so far.

> It all seems to compile and work well except when I attempt to
> terminate my application.  Upon terminating, the thread's call to
> WaitForSingleObject(hThread, INFINITE);  will just sit there
> forever and not do anything.

That is because you are deadlocking the code.  WaitForSingleObject() is
waiting for the thread to terminate, but the thread is waiting for the main
thread to process the Synchronize() requests so that it can then continue on
and eventually detect that it needs to terminate itself.

> If I change the INFINITE to a set value, it will move beyond
> the WaitForSingleObject(hThread, INFINITE); but will
> throw an Access Violation within rtl60.bpl

That is because of your use of FreeOnTerminate.  You are freeing the thread
memory twice.

> if I change the FreeOnTerminate to false it will still hang at
> WaitForSingleObject(hThread, INFINITE); and not move past this.

As well it should.  FreeOnTerminate has nothing to do with Synchronize() or
WaitForSingleObject().  These are completely separate and unrelated issues.


Gambit
Technical Details
Message-ID: <4151128c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <415108cd$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1897390e79ed181f
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <415108cd$1@newsgroups.borland.com>
Subject: Re: Thread WaitFor() ....waits forever
Date: Tue, 21 Sep 2004 22:51:45 -0700
Lines: 61
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4151128c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1095832204 68.69.17.41 (21 Sep 2004 22:50:04 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail