Variables not being reset

Not sure if this is a bug but is definitely something for people to be careful about.

If I have a procedure and in it I declare a local variable eg

function Steve(StringToTest: string): string;
var
sLocalVar: string;
begin
if Pos(':', StringToTest) > 0 then
sLocalVar := 'Fred';

Result := 'Jim ' + sLocalVar;
end;

If I call function Steve and pass in 'aaa : bbb' then I get back 'Jim Fred' as expected.

If I then call Steve AGAIN and pass in 'aaabbb' then I will still get 'Jim Fred' out.

The problem is that sLocalVar is not being reset even though it goes out of scope.

You may say that all local variables should be implicitly initialised but I guess Delphi makes me lazy.

Anyway, one to watch out for as it caused me grief for quite a few hours today.

Cheers

Steve

Comments

  • edited 6:15AM
    Sure, you have to initialize local variables before use them. The same Delphi code may give you AV.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.