DLL problem / application hand / computer crash

Giganews Newsgroups
Subject: DLL problem / application hand / computer crash
Posted by:  Markus (markus.hu…@freenet.de)
Date: 24 Mar 2006


I'm using D2006 architect on Windows XP SP2.
I'm trying to write a communications DLL but have hangs at an early stage.

The dll looks like this:
- initialization: a thread is created and startet, the thread's
  execute method has a loop which runs until terminated is true
- the thread's execute method is a finite state machine with
  2 states already, in one state a new connection is created (so
  the thread own's it) and in the other the thread is normally
  in this state) it has a windows message processing loop like

  if (PeekMessage(msg, 0, 0, 0, pm_remove) = true) then
    if (msg.message = WM_QUIT) then

  Before this loop a self written class for writing to the
  windows event log is instattiated, but I think the problem is
  not here, I already commented it out and the problem stayed.

  Another class for storing data is instantiated as well but
  not yet really used (since no connections are created yet).
  This instance is freed after terminating the loop of the
  execute method. It shouldn't be the cause either, because
  commenting it out did produce the error as well.

- a fialize section which sends terminate to the thread
  and does buisy waiting until the thread signalled via a
  global variable that the execute method realy finished.
  Busy waiting is done like this:

  while (cthreadende = false) and (GetTickCount-t < 1000) do

  where cthreadende is the global var used for signalling.

  After this loop a message is written to the eventlog and
  the log is closed. Normally this message arrives in the
  eventlog! After writing the message the thread is freed
  (freeonterminate is false now)

Now to the problem:
I wrote a VCL based test program which loads the dll via load
library, waits about 150 ms and then frees the dll via
freelibrary. It works in this case but not when you run it in a loop 1000 times:

var dll:THandle;

for i:=1 to 1000 do
  memo1.lines.add('load '+IntToStr(i));
  memo1.lines.add('free '+IntToStr(i));

If run like this it crashes at some random point, normally above 300 iterations. It hangs the program completely, you can try to reset it via Delphi IDE, but that hangs then too and it hangs the explorer so you have to restart the computer.

I tried different things already but I cant see a racing condition or something like this. Any ideas?