Solved

Create and Destroy events in  a unit?

Posted on 2001-08-16
6
148 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 133
Dynamically Created Query 3 55
Delphi and Access based Enumeration 9 63
I want to use librsync in my Delphi backup application. 3 48
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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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