Solved

Variable destroyed

Posted on 2000-03-19
9
245 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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