Solved

Variable destroyed

Posted on 2000-03-19
9
243 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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