Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Variable destroyed

Posted on 2000-03-19
9
Medium Priority
?
252 Views
Last Modified: 2011-09-20
Hi

A embarrassing question how do I tell if a local variable has been destroyed

I thought one of
(assigned(var)), (@var <> nil)
would work but nope :-(
0
Comment
Question by:koger
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2634004
mm.not sure i understand ,is this what your after:

var
s: string;
begin
if assigned(var) then
shomessage('s equals: '+s)
else
showmessage('var is empty');
s := 'hello';
if assigned(var) then
shomessage('s equals: '+s)
else
showmessage('var is still empty');
//last line should never get shown
0
 
LVL 17

Expert Comment

by:inthe
ID: 2634009
how many typos ;-)

var
s: string;
begin
if assigned(s) then
showmessage('s equals: '+s)
else
showmessage('var is empty');
s := 'hello';
if assigned(s) then
showmessage('s equals: '+s)
else
//something went wrong if get here ..
0
 

Author Comment

by:koger
ID: 2634025
huh? :-)
well in this case it is a stringlist

u know

var
  strlst:tstringlist
begin
  create
  then free

  then check if strlst has been destoryed, ofcourse is it that here but in another function I can't tell in designtime :-)

end;
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 17

Expert Comment

by:inthe
ID: 2634043
well i have just made example for a form:
have a look at it while i make example for stringlist:


var
  Form1: TForm1;
formvar : tform;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
if assigned(formvar) then
 begin
  showmessage('formvar is busy');
  exit;
  end
else
 begin
showmessage('formvar is destroyed so i create knew form');
  formvar := tform.Create(self);
  formvar.Left := 0;
  formvar.top := 0;
  formvar.Width := 150;
  formvar.Height := 100;
  formvar.color := clred;
  formvar.show;
  end

 end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 formvar.Destroy;
 formvar := nil
end;
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2634051
You can free the item like this:

strlst.Free;
strlst := Nil;

or like this (with D5)

FreeAndNil(strLst).

Then you can do this:

if not assigned(strlst) then
  // strlst is destroyed.

Cheers,

Raymond.
0
 
LVL 17

Accepted Solution

by:
inthe earned 150 total points
ID: 2634054
i hope i am understanding the question properly and not filling it with rubbish ;-)

try this:

var
  Form1: TForm1;
sl : tstringlist;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
if assigned(sl) then
 begin
  showmessage('sl is full');
  exit;
  end
else
 begin
  showmessage('sl is empty so i full it');
  sl := tstringlist.Create;
  sl.LoadFromFile('c:\autoexec.bat');
 end
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if assigned(sl) then
begin
 sl.Destroy;
 sl := nil
 end
else
showmessage('sl is empty');
end;
0
 

Author Comment

by:koger
ID: 2634093
ahh ok I get you must assign nil to the variable after having destroyed it to make assigned() work.

Just curious is there a way to make it work without assigning nil to the var
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2634106
Not really - you could try grovelling around in the low level memory manager stuff and examine the structures there to determine whether the pointer points to an area of used memory or not, but that is getting pretty nasty.

Assigning nil is simple and easy to do!

Cheers,

Raymond.
0
 

Author Comment

by:koger
ID: 2634116
fine that's what I will do then :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

886 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