Link to home
Start Free TrialLog in
Avatar of alexiat
alexiat

asked on

Run-Time Package Access Violation

I am trying to learn run-time packages using Delphi 2007 on Vista.  The example below works just fine so I am assuming I am doing it correctly.  However, if in the main app I try to unload the package right after the HomePage.Close then I get an access violation in rtl100.bpl.  Why can I unload the package in Destroy but not in the Button1Click?  I'm guessing I'm not doing something correctly!

Thanks.


home10.bpl

unit MyHomePage;

type
  THomePage = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  HomePage: THomePage;

implementation

{$R *.dfm}

procedure THomePage.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

initialization
  RegisterClass (THomePage);

finalization
  UnregisterClass (THomePage);


Main App:

...
  private
    { Private declarations }
    HomePage: TForm;
    FormClass: TFormClass;
    HandlePack: HModule;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation
{$R *.dfm}

procedure TMainForm.Button1Click(Sender: TObject);
begin
  HomePage.Close;
//if I put UnloadPackage here rather than FormDestroy I get an access violation
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  if HandlePack<>0 then UnloadPackage(HandlePack);
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  HandlePack := LoadPackage ('home10.bpl');
  if HandlePack > 0 then begin
    FormClass := TFormClass(GetClass ('THomePage'));
    if Assigned (FormClass) then begin
      HomePage := FormClass.Create (Application);
      HomePage.Show;
      end
    else ShowMessage ('Form class not found');
    end
  else ShowMessage ('Package not found');
end;


Avatar of calinutz
calinutz
Flag of Romania image

Avatar of alexiat
alexiat

ASKER

Thanks.  I have already read these articles plus several others.  What I want to know is the "why" - why it works in one place and not in another.  Guess I will skip the "why" for now.  You can have the points since you were at least kind enough to post a reply to my question.
ASKER CERTIFIED SOLUTION
Avatar of danielluyo
danielluyo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alexiat

ASKER

Thanks.