gpt4 book ai didi

multithreading - 创建 TThread 数组时出现访问冲突

转载 作者:行者123 更新时间:2023-12-03 18:04:19 26 4
gpt4 key购买 nike

我正在创建一个多线程应用程序,它创建了一个类 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.

有什么问题吗?

谢谢。

最佳答案

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

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

关于multithreading - 创建 TThread 数组时出现访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com