Solved

Variable destroyed

Posted on 2000-03-19
9
247 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
[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
  • 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
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!

 
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 50 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 97
Working with hours 3 86
Delphi: ForceDirectory plain function 7 81
Why is there a Difference between the SQL String and using the Parameter ? 7 60
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

737 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