Solved

Destroying Forms.

Posted on 1997-08-10
8
488 Views
Last Modified: 2010-04-04
(A) and (B) are two forms.

I have been using createnew to create a form (B) just before the call to its (B) Show method.  When the form  (B) is closed I then call form(B).release method upon returning to the calling form (A).

I find, by using the 'UBEAUT' new IDE enhancement in DELPHI 3, that the db components (table, datasource,query) have not been freed (as they report as a pointer - not NIL).  This of course means that the form (B) cannot then be freed.

So in the form (B) I have included calls to the various db component's Destroy methods.  I had hoped that this would allow the form (B) to *DIE*.  No such luck.

Next time I need to open this form (B) I check if it is NIL and  if so recreate it.  But it is not NIL so I attempt to access a component (such as a label for changing the forms title or what ever) and BANG the label has a NIL pointer and therefor causes a ACCESS VIOLATION and messes up the application.

Of course one could just leave the form 'ALIVE' until the next time it is needed but this application has some 30 forms and uses  25%-40% of system resources.  I would like to know if it is normal for responsible PC programmers to want to uses a few resource at any one time as possible by  destorying invisible forms or have I spend too much time over the years programming on machines with < 1meg or memory
0
Comment
Question by:Zonnald
8 Comments
 
LVL 3

Expert Comment

by:mirek071497
ID: 1341065
Some source please. I think so you must use :  B.Free;
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1341066
this is from Delphi help, so i think that you play more with source.

"..The CreateNew method creates a new instance of the current form type without reading any state information from resource files or .DFM files. The form is blank, with no controls and all properties at their default. It can be used in place of Create to bypass loading of the associated form file..."
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1341067
I think so when You call release the form was freed but valiable not setted to nil. try :
  B.Release;
  B := nil;
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 1341068
Why are you sure that they aren't freed? Pointers usually aren't set to NIL when memory is freed, so this may be your problem.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:nbtaa2
ID: 1341069
You are calling the Show-methode so the form is modeless, right?
You can't call free, release etc. because you don't know when the form is closed.
You must write this statement in B's FormClose-event:

Action := caFree;

This will free the form from meory and release all of B's controls/components.

-Torfinn

0
 
LVL 1

Author Comment

by:Zonnald
ID: 1341070
Thanks,

The form is modal an I have tried caFree.

With regard to AvonWyss' comment - if it isn't NIL how does one know whether or not it need to be recreated.

Anyway I think that maybe **Win95** may actually be at fault (but don't tell BILL)

I know it is not free as I check using the WATCHES and various other neat debugging tools

Thanks again
0
 
LVL 3

Accepted Solution

by:
sperling earned 50 total points
ID: 1341071
I think you've misunderstood Delphi objects a bit, therefore I try submitting this as an answer.


var
  Form1, Form2 : TForm1;

Form1 := nil;
Form1 := TForm1.Create;  
Form2 := Form1;          
Form1.Free;              

This probably illustrates the problem...

The compiler cannot know how many variables you have pointing to the actual TForm1 instance unless full garbage-collection is implemented. Delphi does not implement garbage-collection for objects, as that would slow everything down quite a bit.

You need to explicitly set Form1 := nil after you've freed it.

E.g.

procedure TForm1.SomeEvent...
begin
  Release;
  Form1 := nil;
end;

This is perfectly legal, as the variable Form1 is simply a pointer to the TForm1 instance. An instance doesn't need to have anything pointing to it, therefore you can e.g. do this

with TForm1.Create(Self) do begin
  ShowModal;
  Release;
end;

The fact that you get an access violation when accessing properties/fields of the form indicates that the form is actually freed, and that Form1 points to unallocated memory.

Regards,

Erik.
0
 
LVL 1

Author Comment

by:Zonnald
ID: 1341072
Thanks I'll try this.

Zonnald
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
loading dll from external path 1 209
Activex get website that activex running on 3 85
Delphi XE10 Round Image 2 75
oracle global variables 4 63
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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

8 Experts available now in Live!

Get 1:1 Help Now