Solved

Create and Destroy events in  a unit?

Posted on 2001-08-16
6
146 Views
Last Modified: 2010-04-06
Hi.

I'm currently writing an app that has loads of different custom Types created in it, so I put them all in a unit to make it easier to share their definitions.

Now what I want to do is create some objects within the unit that are also shared throughout the project.  I need to know how to code an OnCreate event and an OnDestroy event for the unit so that I can create and destroy the objects within the unit.

Could someone advise me please.

Thankyou,

MoonCalf.
0
Comment
Question by:MoonCalf
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 6392790
Why not using the initialization / finalization parts of the unit?

unit blabla;

interface

type TObj1 = class ...;
     TObj2 = class ...;

var obj1 : TObj1 = nil;
    obj2 : TObj2 = nil;

implementation

initialization
  obj1 := TObj1.Create;
  obj2 := TObj2.Create;
finalization
  obj1.Free;
  obj2.Free;
end.

Regards, Madshi.
0
 
LVL 1

Author Comment

by:MoonCalf
ID: 6392804
Madshi,

When I tried that I just used showmessage to tell me when the unit opened and when it closed.

I got the one for opening, but not for closing.

???
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6392816
You don't get one for closing only if someone calls TerminateProcess or ExitProcess. If your process ends typically with closing the main form (that is if the code reaches the last line of your *.dpr file), the finalization parts of all units should be executed...

So the big question is: Who closes your application and how?

Regards, Madshi.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:MoonCalf
ID: 6392826
I close it either from the sysmenu of the form (or the X in the top right corner) or I press Ctrl-F4.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 45 total points
ID: 6392834
Ooops. ShowMessage doesn't show, if Application.Terminated is true - and it is when running through the finalization parts of your units. Use MessageBox instead, it will show you that your program *does* run through the finalization part...

Regards, Madshi.
0
 
LVL 1

Author Comment

by:MoonCalf
ID: 6392884
That's cool.

Thanks Madshi.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now