访问冲突创建TThread数组

时间:2012-05-06 00:59:12

标签: multithreading delphi indy

我正在创建一个多线程应用程序,它创建了一个TThread类的动态数组,但对我而言,它是一个错误,它会导致错误“访问冲突”到“创建”

代码表格:

Unit UNT_Main;

Interface

Uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UNT_Socks;

Type
  TFRM_Main = Class(TForm)
    Procedure FormCreate(Sender: TObject);
  Private
    Procedure OnUpload(Success: Boolean; SockAction: TSockAction);
  Public
    { Public declarations }
  End;

Var
  FRM_Main: TFRM_Main;
  Socks: Array Of TSocks;
  SA: Array Of TSockAction;

Implementation

{$R *.dfm}

Procedure TFRM_Main.OnUpload(Success: Boolean; SockAction: TSockAction);
Begin
  ShowMessage(SockAction.Response);
End;

Procedure TFRM_Main.FormCreate(Sender: TObject);
Var
  I: Integer;
Begin

  SetLength(Socks, 5);
  SetLength(SA, 5);

  For I := 0 To High(Socks)-1 Do
  Begin
    SA[I].SUrl := 'http://google.com.co';
    Socks[I].Create(SA[I]);
    Socks[I].OnUpload := Self.OnUpload;
    Socks[I].Start;
  End;

End;

End.

代码UNT_Socks:

Unit UNT_Socks;

Interface

Uses Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

// action script
Type
  TSockAction = Record
    SUrl: String;
    Response: String;
  End;

  // Eventos
Type
  // on upload event
  TUpload = Procedure(Success: Boolean; SockAction: TSockAction) Of Object;

  // Clase Socks, ejecuta las acciones
Type
  TSocks = Class(TThread)
    // Http indy sock
    Http: TIdHTTP;
    // action script
    FAtnSck: TSockAction;
    // Temp boolean response
    FbTempRet: Boolean;
  Private
    { Eventos }
    FOnUpload: TUpload;
    { Destructor }
    { Metodos & Funciones }
    Function UploadFile: Boolean;
    { Eventos }
    Procedure DoUpload;
  Protected
    Procedure Execute; Override;
  Public
    { Constructor }
    Constructor Create(SockAction: TSockAction);
    { Eventos }
    Property OnUpload: TUpload Read FOnUpload Write FOnUpload;
  End;

Implementation

{ Constructor }
Constructor TSocks.Create(SockAction: TSockAction);
Begin
  Inherited Create(True);

  FAtnSck := SockAction; // <===== Access Violation Here!
  Http := TIdHTTP.Create(Nil);
End;

{ Eventos }
Procedure TSocks.DoUpload;
Begin
  // check if the event is assign
  If Assigned(FOnUpload) Then
    // call it
    FOnUpload(FbTempRet, FAtnSck);
End;

{ Execute }
Procedure TSocks.Execute;
Begin

  FbTempRet := UploadFile;
  Synchronize(DoUpload);

End;

Function TSocks.UploadFile: Boolean;
Var
  SRes: String;
Begin

  Try
    With Http Do
      FAtnSck.Response := Get(FAtnSck.SUrl);
  Except
    Result := False;
  End;

  Result := True;
End;

End.

可能出现什么问题?

谢谢。

3 个答案:

答案 0 :(得分:9)

你的创建循环中有2个错误:

1)当你应该循环到High(Socks)-1时,你正在循环到High(Socks)。您正在分配5个元素的数组,但只初始化其中4个元素。不要在High()中使用-1,而是将其与Length()一起使用。

2)你误导TSocks.Create(),这就是你获得AV的原因。您将其称为实例方法而不是构造函数,但尚未构造任何实例,因此崩溃。

请改用:

For I := 0 To High(Socks) Do
Begin
  SA[I].SUrl := 'http://google.com.co';
  Socks[I] := TSocks.Create(SA[I]); // <-- here
  Socks[I].OnUpload := Self.OnUpload;
  Socks[I].Start;
End;

答案 1 :(得分:2)

而不是

Socks[I].Create(SA[I]);

你应该使用

Socks[I]:= TSocks.Create(SA[I]);

一些小通知:

为什么高(袜子) -1

上传文件的结果总是为真吗?

是否有必要释放Http?

答案 2 :(得分:1)

我同意Remy.also在UNT_Socks中的TSocks有内存泄漏.Http(TIdHttp),FAtnSck(TSockAction)字段在线程终止后没有空闲。

在TSock的consturctor中设置FreeOnTermiate

Constructor TSocks.Create(SockAction: TSockAction);
Begin
  Inherited Create(True);
  FreeOnTerminate:=True;
  FAtnSck := SockAction;  
  Http := TIdHTTP.Create(Nil);
End;

然后覆盖TSock的析构函数

 Destructor TSocks.Destroy;
 Begin

   SockAction.Free; // <===== if needed
   Http.Free;
   Inherited;
 End;