title: Delphi unit initialization not always called
body: <p>I have a unit within a .bpl, and I need a stringlist for a new function that I wrote. I want to the stringlist to persist for the lifetime of the app, so that each call can build on what the prior call found.</p>
  
  <p>So it's declared globally within the unit, and I initialize it in the Initialization section, like this:</p>
  
  <pre><code>var
    ProductLookup : TStrings;  
  ...
  
  function foo : boolean;
  begin
    result := (ProductLookup.IndexOfName('bar') &gt;=0); //blow up here. It's nil. Why?
  end;
  ....
  
  initialization
    ProductLookup := TStringList.Create;  // This should get run, but doesn't.
  
  finalization
    FreeAndNil(ProductLookup);
  
  end.
  </code></pre>
  
  <p>When I unit tested it, everything was fine. But when it gets run from the main app, I was blowing up with an access violation because the stringlist was nil. So now I'm resorting to checking for nil in the foo function and creating if necessary.  But I'm at a loss as to why the initialization isn't working for me. I put a debug message right there in the Initialization, and it doesn't get run when this loads as a BPL, but DOES get run if I compile directly into my dUnit exe.  Any ideas?  Delphi2005.</p>