Hi,

I encountered a very mysterious problem within my code. I have an application which uses threads. There is a class TMyThread which perform some jobs. In the constructor of this class, I set FreeOnTerminate variable to False. My application creates a series for TMyThread, the i-th series is stored in ThreadsForSE [i]. I need to ensure all threads are finished before finishing the application.

I used two approaches for this purpose. The first is to have the code flagged with (1) and remove those flagged with (2). and the other is to remove the line flagged with (1) and keep those flagged with (2). Both approaches seem to be the same, at least for me. But using the second approach, my code get access violation error.

I have not implemented a "Destroy destructor for MyThread class. I am sure that my Collection class is work correctly.

Why two approaches are not functioning the same?

ThreadsCol: TCollection;
ActiveThreadCollection: TCollection;
ActiveThread: TMyThread;

 for i:= 0 to High (ThreadsCol) do
 begin
   ActiveThreadCollection:= ThreadsCol [i];

   for j:= 0 to ActiveThreadCollection.Size- 1 do
   begin
     ActiveThread:= (ActiveThreadCollection.Member [j] as TMyThread);
     if ActiveThread.Suspended then
       ActiveThread.Resume;

     WaitForThreadTerminate (ActiveThread.ThreadID, 0);
(1)     ActiveThread.Free;

   end;

(2)    for j:= 0 to ActiveThreadCollection.Size- 1 do
(2)    begin
(2) ActiveThread:= (ActiveThreadCollection.Member [j] as TAskQueryThread);
(2)      ActiveThread.Free;
(2)
(2)    end;

   ActiveThreadCollection.Clear;
   ActiveThreadCollection.Free;

 end;

_______________________________________________
fpc-devel maillist  -  [email protected]
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to