Bug in IdSSLOpenSSL.pas; fix included

Giganews Newsgroups
Subject: Bug in IdSSLOpenSSL.pas; fix included
Posted by:  Nando Dessena (desse…@sedoc.it)
Date: Wed, 12 Jan 2005

I am using Indy 9.0.18 on Delphi 7, and I have run into a reproducible
error that shows up as "error creating SSL context". Steps:

1- dynamically load the Indy package (through LoadPackage); the
initialization section of IdSSLOpenSSL.pas is executed, but all code is
commented out in there, so no-op:

  // Let's load the library    {Do not Localize}
  //if DLLLoadCount <= 0 then begin
      if not LoadOpenSLLibrary then begin
        raise EIdException.Create(RSOSSLCouldNotLoadSSLLibrary);

2- use it (f. ex. make an HTTPS connection through TIdHTTP +

3- unload the Indy package (through UnloadPackage); the finalization
section of IsSSLOpenSSL.pas is executed, and it unconditionally unloads
the SSL libraries:

  // if DLLLoadCount = 0 then begin
  // end;

4- goto 1: you'll get the aforementioned error at step 2.

The exception is thrown by TIdSSLContext.Create, which does:

constructor TIdSSLContext.Create;
  inherited Create;

  if DLLLoadCount <= 0 then begin
    if not IdSSLOpenSSL.LoadOpenSLLibrary then begin
      raise EIdOSSLCouldNotLoadSSLLibrary.Create


DLLLoadCount is a global variable that is incremented only here and never
decremented. Unloading the package apparently does not reset it. No wonder
that once you unload the SSL libraries with the Indy package they are
never reloaded anymore and thus you get the error. The use of DLLLoadCount
is apparently half-baked, since all the code using that variable is
commented out *except* the portion above.

I see two solutions:

a) restore the use of the counter: increment it in LoadOpenSLLibrary,
decrement it in UnLoadOpenSLLibrary, and never ever look at it from
outside those two functions.

b) comment out the use of the variable in the code above:

  //if DLLLoadCount <= 0 then begin
      if not IdSSLOpenSSL.LoadOpenSLLibrary then begin
        raise EIdOSSLCouldNotLoadSSLLibrary.Create

then you can comment out the variable's declaration as well.

I have applied b) for now and it works for me.

Any chance to see this fixed in an upcoming 9.0.19 maintenance release?
I'd hate having to maintain a private fork.