Here is another question for this multithreading example:
The variable "MyThread" is allocated locally, that means it is allocated on the stack.
As soon as TForm1.FormCreate() exits, the stackframe is released and the variable "MyThread" is invalid.
The tread will access elements, which are stored in the body of this particular instance of the "TMyThread" class.
To my best knowledge, this is a bug.
Or isnt it?
procedure TForm1.FormCreate(Sender: TObject);
var
MyThread : TMyThread;
begin
MyThread := TMyThread.Create(True); // With the True parameter it doesn't start automatically
if Assigned(MyThread.FatalException) then
raise MyThread.FatalException;
// Here the code initialises anything required before the threads starts executing
MyThread.Start;
end;
(I have some aged experience with multithreading in C and decades ago with Borland Pascal and a commercial MT Library on DOS, but not with Object Pascal.)
Edit:
Oops, after reading the manual, I think I understand it.
A class variable actually is a reference (a hidden pointer) to an oject which is allocated on the heap. So this is probably not a bug.
Sorry, I am still learning and probably misleaded by some basic C++ knowledge...
However, the previous bug (busy loop) is true; it can be seen in the task manager.