?
Solved

Create and Destroy events in  a unit?

Posted on 2001-08-16
6
Medium Priority
?
152 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 180 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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