Discussion:
How do I avoid this AV
(too old to reply)
Landiin
2007-10-04 23:42:51 UTC
Permalink
Here is the scenario,

In my component I have a procedure that for loops through a string
list checking for strings. The stringlist is a global and my issue
occores if the component is destroy while in that procedure. What is
happening is the global stringlist is getting freed in the destroy
procedure and when the execution flow exits the destroy procedure at
some point it returns to the procedure that has the for loop in it.
and when it check the string list I get an AV of course because it has
been freed.

Procedure CheckList
var
i: integer;
Begin
Status := 'Checking list';
if bStop then
exit;
for I := 0 to List.Count -1 do <-- Get an AV here destroy is called
after execution is past the bStop check
begin
if List.Strings[I] := 'what erver' then <-- Get an AV here if destroy
is called while in the for loop
begin
do somthing
end;
End;

Deconstructor Destroy
begin
bStop := True;
if assigned(List)
List.free;
inherited;
end;

I know you wouldn't think it would get count at the for loop but it
does. How do I handle this to keep if from creating AVs?

Thanks for any help.
Erich Günthner
2007-10-08 05:54:10 UTC
Permalink
Post by Landiin
Here is the scenario,
In my component I have a procedure that for loops through a string
list checking for strings. The stringlist is a global and my issue
occores if the component is destroy while in that procedure. What is
happening is the global stringlist is getting freed in the destroy
procedure and when the execution flow exits the destroy procedure at
some point it returns to the procedure that has the for loop in it.
and when it check the string list I get an AV of course because it has
been freed.
Procedure CheckList
var
i: integer;
Begin
Status := 'Checking list';
if bStop then
exit;
for I := 0 to List.Count -1 do <-- Get an AV here destroy is called
after execution is past the bStop check
begin
if List.Strings[I] := 'what erver' then <-- Get an AV here if destroy
is called while in the for loop
begin
do somthing
end;
End;
Deconstructor Destroy
begin
bStop := True;
if assigned(List)
List.free;
inherited;
end;
Hello,

Procedure CheckList
var
i: integer;
begin
Status := 'Checking list';

I := 0;

While Assigned(List) and (i < List.count) do
begin
if List.Strings[I] := 'what erver' then
// do something
Inc(i);
end;
End;

Deconstructor Destroy
begin
bStop := True;
if assigned(List)
FreeAndNil(List); // New

inherited;
end;

Of Course there are better ways to solve your problem, but this one
seems to be the fastest way.

Hope it helps.

Erich
Landiin
2007-10-08 20:44:29 UTC
Permalink
Post by Erich Günthner
Post by Landiin
Here is the scenario,
In my component I have a procedure that for loops through a string
list checking for strings. The stringlist is a global and my issue
occores if the component is destroy while in that procedure. What is
happening is the global stringlist is getting freed in the destroy
procedure and when the execution flow exits the destroy procedure at
some point it returns to the procedure that has the for loop in it.
and when it check the string list I get an AV of course because it has
been freed.
Procedure CheckList
var
i: integer;
Begin
Status := 'Checking list';
if bStop then
exit;
for I := 0 to List.Count -1 do <-- Get an AV here destroy is called
after execution is past the bStop check
begin
if List.Strings[I] := 'what erver' then <-- Get an AV here if destroy
is called while in the for loop
begin
do somthing
end;
End;
Deconstructor Destroy
begin
bStop := True;
if assigned(List)
List.free;
inherited;
end;
Hello,
Procedure CheckList
var
i: integer;
begin
Status := 'Checking list';
I := 0;
While Assigned(List) and (i < List.count) do
begin
if List.Strings[I] := 'what erver' then
// do something
Inc(i);
end;
End;
Deconstructor Destroy
begin
bStop := True;
if assigned(List)
FreeAndNil(List); // New
inherited;
end;
Of Course there are better ways to solve your problem, but this one
seems to be the fastest way.
Hope it helps.
Erich- Hide quoted text -
- Show quoted text -
Thanks Erich, I will use your solution. So simple too; guess that
shows my inexperience at programing.
Erich Günthner
2007-10-09 05:53:37 UTC
Permalink
Post by Landiin
Thanks Erich, I will use your solution. So simple too; guess that
shows my inexperience at programing.
We all started as beginners! I also!
(What regards my englisch too)

Erich

Loading...