rayman3411
asked on
DCPCrypt thread issues
I have a very simple function in a DLL. My problem is that the DLL will get an exception every once in a while. The exception is "Invalid Pointer". I get this error when I am calling the DLL from 2 different threads. Is DCPCrypt thread-safe and can I do to insure I am using DCPCrypt correctly.
function SHA1(aInputStr: PChar;
var aHash: PChar): Integer; stdcall;
var
tHash : TDCP_sha1;
sHash : string;
sHex : string;
Digest : array of Byte;
i : integer;
begin
if (State <> READY) and (State <> PUST_IN_PROGRESS) then begin
// Error - DLL is in an invalid state to perform this operation
Result := 1;
Exit;
end;
try
tHash := TDCP_sha1.Create(nil);
try
tHash.Init;
tHash.UpdateStr(StrPas(aInputStr));
// Generate the Hash
SetLength(Digest, tHash.HashSize div 8);
tHash.Final(Digest[0]);
sHash := '';
for i := 0 to Length(Digest) - 1 do begin
sHex := IntToHex(Digest[i], 2);
sHash := sHash + sHex;
end;
if (Get_Memory(Pointer(aHash), Length(sHash))) then begin
// return the CipherText
StrPCopy(aHash, sHash);
Result := 0;
end else begin
Result := 2;
LogInfo('SHA1: ', 'Error allocating memory');
end;
finally
tHash.Free;
end;
except
on E: Exception do begin
State := SHA1_HASH_EXCEPTION;
Result := 2;
LogInfo('SHA1_HASH_EXCEPTION:', E.Message );
end;
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> in the EXCEPT clause of the (nested) TRY statements?
NEVER. trhe except clause executes only on exception. the finally clause executes EVERY time. correct way is
create resource
try
use resource
finally
destroy resource
end;
PS: I don't have time to dig in the DCPcrypt source to check.
NEVER. trhe except clause executes only on exception. the finally clause executes EVERY time. correct way is
create resource
try
use resource
finally
destroy resource
end;
PS: I don't have time to dig in the DCPcrypt source to check.
DCPcrypt - Delphi Cryptography Package (open source / MIT licensed)
http://www.cityinthesky.co.uk/cryptography.html
I haven't found anyone discussing whether it is thread-safe or not.
Is it possible that he needs
tHash.Free;
in the EXCEPT clause of the (nested) TRY statements?