Access violation when creating Thread

1

I'm getting the error:

  

Access violation at address 00420214. Write of address 0000000E.

When creating a Thread. The error line is exactly the creation line (marked with '&'). In uses I added Unit3 (the thread) In var , I set:

DownThread : TMeuDownloader;

And then, at a click of the button, I set:

>DownThread := DownThread.Create(True);
DownThread.FreeOnTerminate := true;
DownThread.Priority := tpNormal;
DownThread.Resume;

And the thread:

unit Unit3;

interface

uses
  Classes;

type
  TMeuDownloader = class(TThread)
  private

  protected
    procedure Execute; override;
  end;

implementation
uses Unit1;
procedure TMeuDownloader.Execute;
begin
end;

end.

Even the thread being empty gives the error.

    
asked by anonymous 12.01.2015 / 15:42

1 answer

5

Where did you write

DownThread := DownThread.Create(True);

The correct one is

DownThread := TMeuDownloader.Create(True);

Notice the difference: in your code you call the Create method from the variable instead of calling it from the class.

Because the variable contains no instance of anything but instead contains only an invalid reference (the variable was not initialized), the method fails to fetch an instance from this invalid reference.

Also, do not use the Resume method to start the thread. Instead, use the Start method.

Your code looks like this:

DownThread := TDownThread.Create(True);
DownThread.FreeOnTerminate := true;
//DownThread.Priority := tpNormal; se é prioridade normal, não precisa informar.
DownThread.Start;
    
12.01.2015 / 16:32