0

Since my iOS app terminates (if running in debugger, the app simply freezes without any errors) after some minutes of intensive use, I started looking into possible reasons why the app would simply suddenly close down. (But always first do so after some time.)

I create lots of shortlived threads that are set to terminate when done. (I use FreeOnTerminate := True in constructor.) However, in Delphi IDE | Thread status the threads seem to live on after execute has run.

So in debug window, I have e.g. 100 threads with State=none and Status=unknown. Am I correct in assuming that means the threads are not completely freed/gone?

For reference, demo code:

constructor TMyOnlineThread.Create(...) ;
begin
  inherited Create(False);
  //--
  //...
  //--
  FreeOnTerminate := True;
end;

destructor TMyOnlineThread.Destroy;
begin
  //...
  inherited;
end;

procedure TMyOnlineThread.Execute;
begin
  //--
  // Single task. No while loop or anything like that.
  //--
  if (...) then
    begin
      if Assigned(FOnDone) then
        Synchronize(MyCallBack)
      ;
    end
  ;
end;

procedure someothercode;
begin
  TMyOnlineThread.Create(...);
end;
Tom
  • 3,587
  • 9
  • 69
  • 124
  • Something I immediately saw was the `inherited;` in your `Execute` procedure - isn't that giving you an abstract error? Remove `inherited;` from `Execute`. – Jerry Dodge Aug 09 '13 at 13:18
  • I also don't see anywhere that you terminate your thread. This should presumably be done at the end of your `Execute` procedure. – Jerry Dodge Aug 09 '13 at 13:21
  • @JerryDodge I set **FreeOnTerminate := True** in the thread constructor. Regarding the call to inherited in **Execute**, it seems Delphi removed the call. (If I use **inherited Execute** I get error) Anyhow, I have now removed the **inherited** line from code both here and in source. – Tom Aug 09 '13 at 13:26
  • `FreeOnTerminate` may be true, but where is it terminated? – Jerry Dodge Aug 09 '13 at 13:27
  • Does that not happen automatically when **Execute** finishes? (There is no while loop in **Excute**. It is a single task.) – Tom Aug 09 '13 at 13:31
  • 2
    Arc in XE4 and threading may not be compatible yet, see [`Delphi TThread under ARC (iOS) not being released`](http://stackoverflow.com/q/17031437/576719), and [`[iOS] TThread not being destroyed under ARC`](http://qc.embarcadero.com/wc/qcmain.aspx?d=116460). – LU RD Aug 09 '13 at 13:31
  • @JerryDodge You will only be getting an `EAbstractError` if you call `inherited` followed by the name of the parent method (`inherited Execute()`). If you omit it and just type `inherited`, no exception will be raised. – Günther the Beautiful Aug 09 '13 at 13:38
  • @GünthertheBeautiful That makes no sense, it does the same exact thing. – Jerry Dodge Aug 09 '13 at 13:39
  • @JerryDodge See discussion + Rudy's (TeamB) reply here: http://embarcadero.public.delphi.oodesign.free-usenet.eu/inherited-vs.-inherited-Create_T33056303_S1 – Tom Aug 09 '13 at 13:54
  • @LURD Thanks for the link. Seems that could be my problem... I guess I will have to consider a design change decision to use a threadpool or something like that :( – Tom Aug 09 '13 at 14:04
  • @JerryDodge It might make "no sense" but that's the way it is, just try it. Keep in mind that an Exception raised in a Thread does not create a Messagebox for the user to see. – Günther the Beautiful Aug 09 '13 at 14:25
  • Doesn't create a messagebox? I get exception messages inside threads all the time... Have you changed the way your IDE reports exceptions? – Jerry Dodge Aug 09 '13 at 15:18
  • @JerryDodge you'll get the first-chance/debugger exception if you're running in the IDE but otherwise unhandled exceptions in threads get swallowed. – J... Aug 09 '13 at 18:06

0 Answers0