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(Sen der: TObject);
begin
HomePage.Close;
//if I put UnloadPackage here rather than FormDestroy I get an access violation
end;
procedure TMainForm.FormDestroy(Send er: 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;
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
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(Sen
begin
HomePage.Close;
//if I put UnloadPackage here rather than FormDestroy I get an access violation
end;
procedure TMainForm.FormDestroy(Send
begin
if HandlePack<>0 then UnloadPackage(HandlePack);
end;
procedure TMainForm.FormShow(Sender:
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;
Check out this also:
http://delphi.about.com/od/objectpascalide/l/aa012301a.htm
http://delphi.about.com/od/objectpascalide/l/aa012301a.htm
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
http://www.obsof.com/delphi_tips/DL613.html
or
http://www.inquiry.com/techtips/delphi_pro/10min/10min0301-1.asp