Access Violation when creating Thread

Asked

Viewed 262 times

1

I’m getting the bug:

Acess 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 the Unit3(thread) In var, i defined:

DownThread : TMeuDownloader;

And then, at one 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 if the thread is empty, it gives the error.

  • Error gives before using "Resume".

1 answer

5


Where you wrote

DownThread := DownThread.Create(True);

the correct is

DownThread := TMeuDownloader.Create(True);

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

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

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

Your code would look like this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.