Access Violation with TIdHttp running on many threads at same time Access Violation with TIdHttp running on many threads at same time multithreading multithreading

Access Violation with TIdHttp running on many threads at same time


You have a multi threaded application. In order for the memory manager to work, multi-threaded applications must set IsMultiThread to True. This will happen if you base your threads on TThread.

From the documentation:

IsMultiThread is set to True to indicate that the memory manager should support multiple threads. IsMultiThread is set to True by BeginThread and class factories.

Because you are calling the raw Windows API CreateThread, and not using the RTL supported thread routines, nothing in the system sets IsMultiThread to True. And so the memory manager assumes that there is only a single thread, and does not lock access to the memory manager's shared data structures. Hence the problems you observed.

If you simply set IsMultiThread := True at startup, your code will work perfectly. Or switch to using a TThread based thread.

Note that your issue has nothing at all to do with Indy. You can see this failure simply by allocating heap memory in the thread. This program dies every time on my system:

{$APPTYPE CONSOLE}uses  SysUtils, Windows;function HttpRequest(AParam : Integer): DWORD; stdcall;var  i: Integer;  P: Pointer;begin  Result := 0;  for i := 1 to 100000 do    GetMem(P, 1);end;var  i: Integer;  tid: DWORD;begin  try    //IsMultiThread := True;//include this line to make program correct    for i := 0 to 15 do      CreateThread(nil, 0, @HttpRequest, nil, 0, tid);  except    on E:Exception do      Writeln(E.Message);  end;  Readln;end.