Solved

Variable destroyed

Posted on 2000-03-19
9
241 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

13 Experts available now in Live!

Get 1:1 Help Now