Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-04

Testing Assigned object

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

SteveG

Is there a simple way to tell if slTest has been assigned in the following ?

(without specifically assigning it to NIL in the declaration)

var
   slTest 	:TStringList;
begin
   if Assigned(slTest) then FreeAndNil(slTest); // gives exception
end;

Thanks - SteveG

Reply by email

SteveG
to Hans-Peter Diettrich: When slTest is a local variable, you should initialize it to Nil. Otherwise the variable…
Hans-Peter Diettrich wrote 18 lines
SteveG schrieb: > Is there a simple way to tell if slTest has been assigned in the > following ? > > (without specifically assigning it to NIL in the declaration) > > var > slTest :TStringList; > > begin > if Assigned(slTest) then FreeAndNil(slTest); // gives exception > end; When slTest is a local variable, you should initialize it to Nil. Otherwise the variable contains garbage, that most probably is *not* Nil, so that FreeAndNil tries to free something that never has been created.

Shall do - Thanks for that

Reply by email

Hans-Peter Diettrich
to SteveG: Is there a simple way to tell if slTest has been assigned in the following ? (without…
Show quoted text 11 lines
Is there a simple way to tell if slTest has been assigned in the following ? (without specifically assigning it to NIL in the declaration) var slTest :TStringList; begin if Assigned(slTest) then FreeAndNil(slTest); // gives exception end;

When slTest is a local variable, you should initialize it to Nil. Otherwise the variable contains garbage, that most probably is *not* Nil, so that FreeAndNil tries to free something that never has been created.

DoDi

Reply by email

Marco van de Voort
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote 10 lines
> (without specifically assigning it to NIL in the declaration) > > var > slTest :TStringList; > > begin > if Assigned(slTest) then FreeAndNil(slTest); // gives exception > end; When slTest is a local variable, you should initialize it to Nil.

Just to be clear

Initialize it in code not in the declaration, since in the declaration afaik that makes it a static variable instead of a truely local one, even when declared with local scope.

Reply by email

Sven Barth
to Marco van de Voort
Marco van de Voort wrote 17 lines
On Mon, Apr 25, 2011 at 10:15:57AM +0200, Hans-Peter Diettrich wrote: >> (without specifically assigning it to NIL in the declaration) >> >> var >> slTest :TStringList; >> >> begin >> if Assigned(slTest) then FreeAndNil(slTest); // gives exception >> end; > > When slTest is a local variable, you should initialize it to Nil. Just to be clear Initialize it in code not in the declaration, since in the declaration afaik that makes it a static variable instead of a truely local one, even when declared with local scope.

You can only initialize a global variable. For "initialized local variables" you need to use writable "constants" ;)

Regardts,
Sven

Reply by email

cobines
to Marco van de Voort: Just to be clear Initialize it in code not in the declaration, since in the declaration…

2011/4/25 Marco van de Voort :

Show quoted text 5 lines
Just to be clear Initialize it in code not in the declaration, since in the declaration afaik that makes it a static variable instead of a truely local one, even when declared with local scope.
procedure Test;
var
  i: Integer = 0;
  p: Pointer = nil;
  t: TObject = nil;
begin
end;

These are not local variables? If not, what are the consequences of them being static variables? I assume it is worse, since you recommend otherwise.

cobines

Reply by email

Bernd
to Marco van de Voort: Just to be clear Initialize it in code not in the declaration, since in the declaration…

2011/4/25 Marco van de Voort :

Initialize it in code not in the declaration, since in the declaration afaik that makes it a static variable instead of a truely local one, even when declared with local scope.

Hmm... are you sure?

program project1;
{$mode objfpc}{$H+}
procedure print(num: Integer);
begin
  writeln(num);
end;
procedure foo(cnt: Integer {-4});
var
  x : integer = 0;    {-8}
  y : integer;        {-12}
begin
  print(x);
  print(y);
  inc(x);
  inc(y);
  if cnt < 5 then begin
    foo(cnt + 1);
  end;
end;
begin
  foo(0);
end.

compiled with FPC 2.4.2

P$PROJECT1_FOO$LONGINT:
pushl %ebp
movl %esp,%ebp
subl $12,%esp
movl %eax,-4(%ebp)

movl TC_P$PROJECT1_FOO$LONGINT_defaultx,%eax
movl %eax,-8(%ebp)

movl -8(%ebp),%eax
call P$PROJECT1_PRINT$LONGINT
movl -12(%ebp),%eax
call P$PROJECT1_PRINT$LONGINT

incl -8(%ebp)
incl -12(%ebp)

movl -4(%ebp),%eax
cmpl $5,%eax
jl .Lj23
jmp .Lj24
.Lj23:
movl -4(%ebp),%eax
incl %eax
call P$PROJECT1_FOO$LONGINT
.Lj24:
leave
ret

output:
0
2
0
134512851
0
134512851
0
134512851
0
134512851
0
134512851

Reply by email

Marco van de Voort
to Bernd
Bernd wrote
> that makes it a static variable instead of a truely local one, even when declared > with local scope. Hmm... are you sure?

Then, yes. Now, no :-)

I assumed it was the same as the const variant.

Reply by email