TThread in Delphi DLL's with initialization/finalization

Hello,

I'm using a TThread in a (Delphi XE) DLL.

In the initialization section of a unit, I create the thread and in the
finalization section I would like to free/release/terminate the thread.

I've used this code in normal executables and then it works without any
problem.

But when I use this code in a DLL (ex. ISAPI dll), then this go wrong....

When I Free the thread in de finalization section, the finalisation/free
routine will completly lock up.
 (See example 'DLLThreadBlocking' in attached archive)

When I try a different approach, to Terminate the thread and set
'FreeOnTerminate := True', the Thread will be freed, but there will be a
memory leak. (See example 'DLLThreadMemoryLeak').

The archive containt both compiled dll's and a 'loader', which will try to
load/unload the dll.
When the dll is loaded is will show a dialog. As soon as OK is clicked the
dll is unloaded.

But anyway, it looks like something is not working correctly.
(or I'm using thread in a way, that the Os/Delphi doesn't support...)

-- 
Regards,

Ronald Hoek

Application Developer
ComponentAgro B.V.
Oud-Beijerland - The Netherlands

http://www.componentagro.nl

KvK: H24264020
0
Ronald
3/23/2011 1:55:37 PM
📁 embarcadero.delphi.general
📃 4258 articles.
⭐ 0 followers.

💬 5 Replies
👁️‍🗨️ 1889 Views

Hi,

Eventually it is the same problem the guys at RemObjects had:
http://blogs.remobjects.com/blogs/ck/2006/10/05/p121

I would suggest you add to additional functions to your
dll - an init and finalize function (like for COM uses
CoInitialize and CoUninitialize ;).

Call them just after you loaded the dll and before you unload it...
then you never have problems like that ;)

kind regards
    Mike
0
Michael
3/23/2011 4:07:15 PM
Ronald Hoek wrote:

> In the initialization section of a unit, I create the thread and in
> the finalization section I would like to free/release/terminate the
> thread.

Take a look:

http://download.microsoft.com/download/a/f/7/af7777e5-7dcd-4800-8a0a-b18336565f5b/DLL_bestprac.doc

--
Alex
0
Alex
3/23/2011 5:05:20 PM
"Ronald Hoek" <none> wrote in message news:340373@forums.embarcadero.com...

> In the initialization section of a unit, I create the thread and in the
> finalization section I would like to free/release/terminate the thread.

That is not a good thing to do inside a DLL.  You should export public 
functions for the app to call when needed instead.

-- 
Remy Lebeau (TeamB)
0
Remy
3/23/2011 7:09:40 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> "Ronald Hoek" <none> wrote in message news:340373@forums.embarcadero.com...
> 
> > In the initialization section of a unit, I create the thread and in the
> > finalization section I would like to free/release/terminate the thread.
> 
> That is not a good thing to do inside a DLL.  You should export public 
> functions for the app to call when needed instead.
> 
> -- 
> Remy Lebeau (TeamB)

Being this is an ISAPI DLL,  not much of this helps.  

The solution is to terminate your thread and waitfor in the ISAPI "TerminateExtension" procedure. This works great.  And as per Microsoft  "TerminateExtension provides a place to put code that cleans up threads or de-allocate resources"
0
Tod
9/5/2011 1:48:41 AM
Thanks for the info everybody!!!

-- 
Regards,

Ronald Hoek

Application Developer
ComponentAgro B.V.
Oud-Beijerland - The Netherlands

http://www.componentagro.nl

KvK: H24264020
0
Ronald
9/7/2011 8:27:17 AM
Reply: