Solved

Create and Destroy events in  a unit?

Posted on 2001-08-16
6
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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