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
Darian reminds me that I've answered this before:
If the operating system loads the BPL as part of loading the associated EXE, then not all the initialization sections will get called. Instead, only the sections from the units that are explicitly used by something else in the program get called.
If the code in the initialization section registers a class, and then you only refer to that class indirectly, say by looking for it by name in a list, then the unit's initialization section might not get called. Adding that unit to any "uses" clause in your program should solve that problem.
To work around this problem, you can initialize the package's units yourself by calling the InitializePackage function, in the SysUtils unit. It requires a module handle, which you can get by calling the GetModuleHandle API function. That function will only call the initialization sections of the units that haven't already been initialized. That's my observation, anyway.
If you call
InitializePackage, then you should also callFinalizePackage. When your package gets unloaded, the finalization sections will get called for all the units that were automatically initialized.If the OS does not automatically load your package, then you are loading it with the
LoadPackagefunction. It initializes all the package's units for you, so you don't need to callInitializePackageyourself. Likewise,UnloadPackagewill finalize everything for you.