1

I'm using Delphi Rio. I have created a thread class.

type
  TThreadManager = class(TThread)
    constructor Create;
  end;

constructor TThreadManager.Create;
begin
  inherited Create(True);     // thread-ul va fi creat, dar nu va rula
                               // pentru a-l rula, folosim "Resume" (sau "Execute")
  FreeOnTerminate := True;     // thread-ul va fi distrus automat cand termina
  Priority        := tpNormal; // prioritatea thread-ului este minima
  <- creating internal objects here
  fIsSetupOk      := False;
end;

However (this) constructor is not used when I create the thread in the application. No debug breakpoints are available. And also no objects are created.

threadManager := TThreadManager.Create;
threadManager.Setup(dmMain.ibSessionMain);
threadManager.Resume;

Because of not entering this constructor, an AV is raised when accessing the objects.

Any hints?

Sure, I can create the objects elsewhere (into the setup) but that is not what I want.

2
  • 2
    I don't see anything obviously wrong in your code. The error is probably in a part that you don't show. Please provide a minimal, complete, verifiable and reproducible example. With such example, we will be able to reproduce your problem, understand it and try to solve it. Commented Sep 4, 2021 at 7:01
  • Rightclick on TThreadManager and choose "Find declaration" - do you really see the code you expect or is it a different one? Also try TheUnitName.TThreadManager.Create to make it unambiguous. Commented Sep 4, 2021 at 7:24

2 Answers 2

3

TThread has its own parameter-less Create() constructor. You should declare yours as reintroduce to hide the existing one, eg:

type
  TThreadManager = class(TThread)
  public
    constructor Create; reintroduce;
  end;

A better option would be to call Setup() inside of Create() itself, and set CreateSuspended=False. This way, there is no ambiguity over which constructor to call, and the thread will start running automatically after Setup() is finished, eg:

type
  TThreadManager = class(TThread)
  public
    constructor Create(Session: TIB_Session); reintroduce;
  end;

constructor TThreadManager.Create(Session: TIB_Session);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  Priority := tpNormal;
  <- creating internal objects here
  fIsSetupOk := False;
  Setup(Session);
end;

threadManager := TThreadManager.Create(dmMain.ibSessionMain);

Alternatively, you can rename your constructor to something more meaningful, eg:

type
  TThreadManager = class(TThread)
  public
    constructor CreateAndSetup(Session: TIB_Session);
  end;

constructor TThreadManager.CreateAndSetup(Session: TIB_Session);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  Priority := tpNormal;
  <- creating internal objects here
  fIsSetupOk := False;
  Setup(Session);
end;

threadManager := TThreadManager.CreateAndSetup(dmMain.ibSessionMain);
Sign up to request clarification or add additional context in comments.

Comments

0

I have found the problem in my code after I use suggestion 2 from remy.
After creating the method as suggested (adding TIB_SESSION as parameter) I have found that the method is not usable because is declared in the protected section not public.
After putting in the correct section all is fine.
Many tks

4 Comments

Then the code you showed us is wrong. The concept of a MCVE is that you show us what you executed yourself. Since that code did not have protected in its class definition you misleaded everyone.
Indeed. however I have showed what I think is enough. I have already admitted my fault, and I put the correct answer for anyone else see this or have similar problems.
Concept on minimal reproducible example is not that you show what you think is enough, but to create project with that reduced code and that you test it to see whether problem exists. Adding that information as answer is not as helpful to others as you may think. It merely serves as reminder that you didn't make proper mcve and why it is important to do that before asking questions.
You would need to make your custom constructor public only if TThreadManager is implemented in a different unit than it it used in. Which was not made clear in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.