Recent

Author Topic: [SOLVED] Application still exist in Windows Task Manager after terminated  (Read 633 times)

incendio

  • Sr. Member
  • ****
  • Posts: 308
Hi guys,

I have an application in Windows OS that still active in task manager even already  terminated.

Here are the sample of my codes :
Code: Pascal  [Select][+][-]
  1. procedure Run(Opt : string);
  2. var
  3.    Rsl : string;
  4. begin
  5.    try
  6.       if opt = 'a' then Rsl := RunFunc1
  7.       else if opt = 'b' then Rsl:=RunFunc2;
  8.    finally
  9.       application.Terminate;
  10.    end;
  11. end;
  12.  

RunFunc1, application terminated and not exist in task manager, but RunFunc2, application closed, but in task manager, still active.

I couldn't find what is wrong with RunFunc2

Here are the codes for RunFunc2
Code: Pascal  [Select][+][-]
  1. var
  2.   WB        : TsWorkbook;
  3.   WS        : TsWorksheet;
  4.   CmpnyNm   : string;
  5.   RptTitle  : string;
  6.   Row,i     : integer;
  7.   Cell      : PCell;
  8.   NoOfCol   : integer;
  9.   SumOfCol  : array of double;
  10.   ColHeader : array of string;
  11.   RptId     : String;
  12.   Str       : Tstringlist;
  13.   CoaCd     : String;
  14.   FrCoaCd   : String;
  15.   ToCoaCd   : String;
  16.   TtlDb,TtlCr : double;
  17.  
  18. begin
  19.   try
  20.     WB       := TsWorkbook.Create;
  21.     WS       := WB.AddWorksheet(RptNm.Substring(0,30));
  22.     Row      := 0;
  23.  
  24.     GetCoaCd(RptNm,FrCoaCd,ToCoaCd);
  25.  
  26.     GetRptIdCmpnyTtl(RptNm,RptId,CmpnyNm,RptTitle);
  27.     NoOfCol  := 6;
  28.     ColHeader:= GetColHeader(RptId,NoOfCol);
  29.  
  30.     SetLength(SumOfCol, NoOfCol);
  31.     for i:= 0 to NoOfCol-1 do SumOfCol[i] := 0;
  32.  
  33.     WS.Options:= ws.Options - [soShowGridlines];
  34.     WriteCmpnyAndTitle(Ws,CmpnyNm,RptTitle,FrDt,ToDt,Row);
  35.  
  36.     CoaCd := '';
  37.     TtlDb := 0;
  38.     TtlCr := 0;
  39.  
  40.     DM.Q2.Close;
  41.     DM.Q2.SQL.Clear;
  42.     DM.Q2.SQL.Add('SELECT COUNT(*) ttl FROM ACCT.Func2 (0'+ ',''' + FrCoaCd + ''',''' + ToCoaCd + ''',''' + FrDt + ''',''' + ToDt + ''')' );
  43.     DM.Q2.Open;
  44.  
  45.     P.Value     := 0;
  46.     p.MaxValue  := DM.Q2.FieldByName('ttl').AsInteger;
  47.  
  48.     DM.Q2.Close;
  49.     DM.Q2.SQL.Clear;
  50.     DM.Q2.SQL.Add('SELECT COACD,AC,DT,FRMN,DSCP,DB,CR,BLNC FROM ACCT.Func2  (0' + ',''' + FrCoaCd + ''',''' + ToCoaCd + ''',''' + FrDt + ''',''' + ToDt + ''')' );
  51.     DM.Q2.Open;
  52.  
  53.     txtLbl.Visible:=true;
  54.  
  55.     while not(DM.Q2.EOF) do
  56.     begin
  57.       if CoaCd='' then
  58.       begin
  59.          CoaCd := DM.Q2.FieldByName('COACD').AsString;
  60.          TtlDb := TtlDb + DM.Q2.FieldByName('DB').AsFloat;
  61.          TtlCr := TtlCr + DM.Q2.FieldByName('CR').AsFloat;
  62.  
  63.          WS.WriteText(Row,0,CoaCd);
  64.          ws.WriteFontStyle(Row, 0,[fssBold]);
  65.          ws.WriteFontColor(Row, 0,$00000080);
  66.  
  67.          Row := Row+1;
  68.          WS.WriteText(Row,0,DM.Q2.FieldByName('AC').AsString);
  69.          ws.WriteFontStyle(Row, 0,[fssBold]);
  70.          ws.WriteFontColor(Row, 0,$00000080);
  71.  
  72.          WriteHeader(WS,RptId,Row,ColHeader);
  73.          Row   := Row+1;
  74.  
  75.          WriteCellValue2(WS,2,Row,DM.Q2,false,SumOfCol);
  76.          Row   := Row + 1;
  77.       end
  78.       else if DM.Q2.FieldByName('COACD').AsString <> CoaCd then
  79.       begin
  80.          for i:= 0 to 5 do
  81.          begin
  82.            Cell := ws.WriteBorders(Row,i,[cbNorth]);
  83.            WS.WriteBorderStyle(cell, cbNorth, lsThin, scBlack);
  84.          end;
  85.  
  86.          WS.WriteNumber(Row,3,TtlDb,nfCustom,NumFormat);
  87.          WS.WriteNumber(Row,4,TtlCr,nfCustom,NumFormat);
  88.  
  89.          ws.WriteFontStyle(Row, 3,[fssBold]);
  90.          ws.WriteFontStyle(Row, 4,[fssBold]);
  91.  
  92.          CoaCd := DM.Q2.FieldByName('COACD').AsString;
  93.          TtlDb := 0;
  94.          TtlCr := 0;
  95.          Row   := Row+1;
  96.  
  97.          WS.WriteText(Row,0,CoaCd);
  98.          ws.WriteFontStyle(Row, 0,[fssBold]);
  99.          ws.WriteFontColor(Row, 0,$00000080);
  100.  
  101.          Row := Row+1;
  102.          WS.WriteText(Row,0,DM.Q2.FieldByName('AC').AsString);
  103.          ws.WriteFontStyle(Row, 0,[fssBold]);
  104.          ws.WriteFontColor(Row, 0,$00000080);
  105.  
  106.          WriteHeader(WS,RptId,Row,ColHeader);
  107.          Row   := Row+1;
  108.  
  109.          WriteCellValue2(WS,2,Row,DM.Q2,false,SumOfCol);
  110.          Row   := Row + 1;
  111.          TtlDb := TtlDb + DM.Q2.FieldByName('DB').AsFloat;
  112.          TtlCr := TtlCr + DM.Q2.FieldByName('CR').AsFloat;
  113.  
  114.       end
  115.       else
  116.       begin
  117.         WriteCellValue2(WS,2,Row,DM.Q2,false,SumOfCol);
  118.         Row   := Row + 1;
  119.         TtlDb := TtlDb + DM.Q2.FieldByName('DB').AsFloat;
  120.         TtlCr := TtlCr + DM.Q2.FieldByName('CR').AsFloat;
  121.       end;
  122.  
  123.       P.Value := P.Value+1;
  124.       txtLbl.Caption:= DM.Q2.FieldByName('AC').AsString;
  125.       application.ProcessMessages;
  126.       DM.Q2.Next
  127.     end;
  128.  
  129.     for i:= 0 to 5 do
  130.     begin
  131.       Cell := ws.WriteBorders(Row,i,[cbNorth]);
  132.       WS.WriteBorderStyle(cell, cbNorth, lsThin, scBlack);
  133.     end;
  134.  
  135.     WS.WriteNumber(Row,3,TtlDb,nfCustom,NumFormat);
  136.     WS.WriteNumber(Row,4,TtlCr,nfCustom,NumFormat);
  137.  
  138.     ws.WriteFontStyle(Row, 3,[fssBold]);
  139.     ws.WriteFontStyle(Row, 4,[fssBold]);
  140.  
  141.     WB.WriteToFile(RptNm + '.xlsx',true);
  142.     OpenDocument(RptNm + '.xlsx');
  143.     Result := 'OK';
  144.   except
  145.       on E: Exception do
  146.       begin
  147.         if E.Message.Contains('it is being used') then Result := 'Failed'
  148.         else Result := E.Message;
  149.  
  150.         Str := TStringlist.create;
  151.         Str.Add(E.Message);
  152.         Str.SaveToFile(RptNm + '.err');
  153.         Str.Free;
  154.       end;
  155.   end;
  156.   WB.Free;
  157.   txtLbl.Visible:=false;
  158. end;
  159.  
  160.  
Can someone give me a hint, why RunProc2 still exist in Window Task Manager even it was terminated?
« Last Edit: April 30, 2025, 05:35:25 am by incendio »

Khrys

  • Full Member
  • ***
  • Posts: 227
Re: Application still exist in Windows Task Manager after terminated
« Reply #1 on: April 29, 2025, 07:18:35 am »
You could attach a debugger to the running process to find out where it's stuck.
Lazarus comes with a copy of GDB located at  C:\lazarus\mingw\x86_64-win64\bin\gdb.exe.

After finding the relevant PID, you can attach GDB to the process using  gdb --pid <PID>,  then run  thr app all bt  to show the call stack for all running threads (although thread 1 is the most relevant in an LCL application).

silvercoder70

  • Full Member
  • ***
  • Posts: 180
    • Tim Coates
Re: Application still exist in Windows Task Manager after terminated
« Reply #2 on: April 29, 2025, 01:14:03 pm »
Two thoughts...

1. Add some debugging code using OutputDebugString(...). But it sounds like from what you are saying the .xls is created?

2. Open the document (.xls) after you have freed the workbook.

Hope this helps.
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

incendio

  • Sr. Member
  • ****
  • Posts: 308
Re: Application still exist in Windows Task Manager after terminated
« Reply #3 on: April 30, 2025, 03:28:33 am »
I was able to run my app within Lazarus IDE with a debuger.

The code that caused error was the line outside try catch block.
Code: Pascal  [Select][+][-]
  1. WB.Free


Error message was : EXTERNAL ACCESS VIOLATION

Open the document then call WB.Free caused the same error.

I have other functions that called WB.Free, none of them give the error.

Removed a call to WB.Free, app runs as expected.

incendio

  • Sr. Member
  • ****
  • Posts: 308
Re: Application still exist in Windows Task Manager after terminated
« Reply #4 on: April 30, 2025, 05:35:08 am »
Found the problem!

The error caused by incorrect size of an array.

Here is the codes in one of my function that caused error
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.WriteCellValue2(WS : TsWorksheet; StartCol: integer;var Row: integer; Q: TIBQuery;
  2.                          LastRow : boolean;var SumOfCol : array of double);
  3. var
  4.   i       : integer;
  5.   Cell    : PCell;
  6.   NoOfCol : integer;
  7.   Col     : integer;
  8.  
  9. begin
  10.   NoOfCol := Q.FieldCount;
  11.   for i:= StartCol to NoOfCol - 1 do
  12.   begin
  13.     Col := i-StartCol;
  14.     if ( not Q.Fields.Fields[i].IsNull) then
  15.     begin
  16.        if (Q.Fields.Fields[i].DataType = ftDateTime) or (Q.Fields.Fields[i].DataType = ftDate) then WS.WriteText(Row,Col,FormatDateTime('dd-mm-yy',Q.Fields.Fields[i].AsDateTime))
  17.        else if (Q.Fields.Fields[i].DataType = ftString) or
  18.                (Q.Fields.Fields[i].DataType = ftWideString) or
  19.                (Q.Fields.Fields[i].DataType = ftFixedChar) then WS.WriteText(Row,Col,Q.Fields.Fields[i].AsString)
  20.        else if (Q.Fields.Fields[i].DataType = ftSmallint) or
  21.                (Q.Fields.Fields[i].DataType = ftInteger) or
  22.                (Q.Fields.Fields[i].DataType = ftLargeint) or
  23.                (Q.Fields.Fields[i].DataType = ftBCD) or
  24.                (Q.Fields.Fields[i].DataType = ftFloat) then
  25.        begin
  26.          WS.WriteNumber(Row,Col,Q.Fields.Fields[i].AsFloat,nfCustom,NumFormat);
  27.          SumOfCol[i] := SumOfCol[i] + Q.Fields.Fields[i].AsFloat;
  28.        end;
  29.     end;
  30.  
  31.     Cell := ws.WriteBorders(Row,Col,[cbNorth, cbSouth,cbWest, cbEast]);
  32.     WS.WriteBorderStyle(cell, cbNorth, lsHair, scBlack);
  33.     WS.WriteBorderStyle(cell, cbSouth, lsHair, scBlack);
  34.     WS.WriteBorderStyle(cell, cbEast, lsThin, scBlack);
  35.     WS.WriteBorderStyle(cell, cbEast, lsThin, scBlack);
  36.  
  37.     if LastRow then WS.WriteBorderStyle(cell, cbSouth, lsThin, scBlack);
  38.   end;
  39. end;
  40.  

SumOfCol size should be 8, but I set it to 6, strange, how this error could cause Workbook to crashed since it has nothing to do with a Workbook.

d2010

  • Full Member
  • ***
  • Posts: 160
Re: Application still exist in Windows Task Manager after terminated
« Reply #5 on: April 30, 2025, 07:01:19 am »
Found the problem!
Here is the codes in one of my function that caused error
Code: Pascal  [Select][+][-]
  1. You define one/two variabiles
  2. Const MyDblEnd=1978010112.3244;
  3.        SumOfCol[06]:=MyDblEnd;
  4. Here after [06] you break , and rest all values will be igonred.
  5.  :(
  6. [code]
  7. for idc:=low(SumOfCol) to length( SumOfCol) do
  8.    Begin if ( SumOfCol[idc]=MyDblEnd) then Begin
  9. writeln(' I forced exists any time');
  10. system.break
  11.     End;
  12.     End;
  13.  
« Last Edit: April 30, 2025, 07:03:32 am by d2010 »

 

TinyPortal © 2005-2018