Since Delphi 3, NotifyModuleUnload in the System unit has been written the way it is now.

procedure NotifyModuleUnload(HInstance: THandle);
var
  P: PModuleUnloadRec;
begin
  P := ModuleUnloadList;
  while P <> nil do
  begin
    try
      P.Proc(HInstance);
    except
      // Make sure it doesn't stop notifications
    end;
    P := P.Next;
  end;
{$IFDEF LINUX}
  InvalidateModuleCache;
{$ENDIF}
end;

The IDE calls NotifyModuleUnload in these conditions:

  • after units have been unloaded (via UnregisterModule)
  • when unloading a package