function CurlIsAvailable: boolean;
begin
try
if curl.Module=0 then
LibCurlInitialize;
result := PtrInt(curl.Module)>0;
except
result := false;
end;
end;
Now, consider situation when thread A and B calls it at same time (library is not initialized yet).
1. Thread A compares curl.Module with zero and call LibCurlInitialize.
2. Thread A enters into critical section inside LibCurlInitialize and load dll into memory (call LoadLibrary), now curl.Module <> 0
3. Thread A interrupted halfway (no any GetProcAddress called yet) and Thread B start its work
4. Thread B compares curl.Module with zero and return True
5. Thread B try access to the any function (for example, curl.easy_init) and fails with AV!
To fix this bug we need to use temporary variable for library handle instead immediately writing to curl.Module.
curl.Module should be written only at the exit from LibCurlInitialize when initialization is fully complete.