Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create and Destroy events in  a unit?

Posted on 2001-08-16
6
Medium Priority
?
154 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 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