i have a test.bpl package in my application and it has a form named myPackageForm. after loading my Package when i want to close myPackageForm, Application will Terminate.
Main Project Initialization:
Application.Initialize;
Application.CreateForm(TMainForm,MainForm);
Application.Run;
MainForm FormCreate :
aPackage := LoadPackage('my bpl path'+test.bpl);
@P_ItemClick := GetProcAddress(aPackage,'ItemClickExcecute');
MainForm ButtonClick:
P_ItemClick(Sender);
testPackage CommandsUnit :
Procedure ItemClickExecute(Sender : TObject)
begin
TmyPackageForm.ShowForm();
end;
exports
ItemClickExecute;
end.
myPackagForm has a class procedure to show it:
Class Procedure TmyPackagForm.ShowForm;
begin
if not Assigned(myPackagForm)
myPackagForm := Application.CreateForm(TmyPackagForm ,myPackagForm );
myPackagForm.Show;
end;
And in OnClose Procedure has:
Release;
As a solution i try another command:
myPackagForm := TmyPackagForm.Create(Application);
to myPackagForm creation;
Can anybody know what happend in Delphi XE4 with CreateForm command on the packages?
Sir Rufo has got the basics covered. The first form created with a call to
CreateForm
becomes the main form. When the main form closes, the application closes.Now, you have shown two calls to
CreateForm
. One in the host application, and one in your dynamically loaded package. And it seems clear that the first one to run is from the host application. So, how can the form in the package can bring down the application? Well, that can only happen, as Sir Rufo said, if there are two application instances in your process.So the next question is, how can there be two application instances in your process? That should not happen. The entire point of packages is to allow Delphi units to be shared between different modules. If you have built your application correctly then you will have only one instance of
TApplication
that is shared between the host executable, and all of your packages.So, the only reasonable conclusion to reach is that one of your modules is not built using runtime packages. For instance, perhaps the host application includes the RTL/VCL, linked statically inside the executable file. And your packages link to the RTL/VCL runtime packages. Or perhaps it is the dynamically loaded package that includes the RTL/VCL linked statically. Although IIRC, the compiler prevents that happening.
Whatever has gone wrong, the solution is that all your modules must be built using runtime packages. All the RTL/VCL libraries must be linked via runtime packages and must not be linked statically.
So, replacing
Application.CreateForm
withTMyPackageForm.Create
is simply suppressing a wider problem. It is critical that you have only one instance of the RTL/VCL in your application. Achieve that by using runtime packages in all your modules.It is certainly the case that
TMyPackageForm.Create
is the right way to create a form in your package. I only useApplication.CreateForm
for the main form. I never use it anywhere else. But don't try to fix your immediate problem solely by removingApplication.CreateForm
from your package. Fix the linking to the RTL/VCL.