Sign in Join

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

Mailing lists / fpc-pascal / 2014-05

For-in-loop question about GetEnumerator

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Krzysztof

Hi,

This article is quite clear for me: http://wiki.freepascal.org/for-in_loop

But let say that I have this class:

TMyList = class
private
  FList: TList
public
  constructor Create;
  destructor Destroy;
  procedure Add;
  procedure Remove;
end;

Now I want to do:

for a in MyClass do
begin
end;

Can I do this without creating new enumerator? I mean pass enumerator from FList? I'm confused because if I create function:

function TMyClass.GetEnumerator: TListEnumerator
begin
  Result := FList.GetEnumerator;
end;

... then FList.GetEnumerator create new object
(TListEnumerator.Create(Self)). My question is, where it is freed? Is it freed automatically on runtime after exit from loop? I don't want memory leak

Regards

Reply by email

silvioprog
to Krzysztof: This article is quite clear for me: http://wiki.freepascal.org/for-in_loop But let say…

2014-05-16 18:13 GMT-03:00 Krzysztof :

Show quoted text 39 lines
Hi, This article is quite clear for me: http://wiki.freepascal.org/for-in_loop But let say that I have this class: TMyList = class private FList: TList public constructor Create; destructor Destroy; procedure Add; procedure Remove; end; Now I want to do: for a in MyClass do begin end; Can I do this without creating new enumerator? I mean pass enumerator from FList? I'm confused because if I create function: function TMyClass.GetEnumerator: TListEnumerator begin Result := FList.GetEnumerator; end; ... then FList.GetEnumerator create new object (TListEnumerator.Create(Self)). My question is, where it is freed? Is it freed automatically on runtime after exit from loop? I don't want memory leak Regards

You need to create a enumerator, and it will be freed by your owner. See internal implementation:

Function  TComponent.GetEnumerator: TComponentEnumerator;
begin
  Result:=TComponentEnumerator.Create(Self); // Self is the owner
component, and it will free your enumerator automatically in destroy
end;
Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Jürgen Hestermann
to Mattias Gaertner
Mattias Gaertner wrote
I added a note to the example, that the enumerator is automatically freed by the compiler after the loop.

I just had a look at the UTF8 example on http://wiki.freepascal.org/for-in_loop where is says:

------------------------------------------------
for i := 1 to Length(s) do
      DoSomething(ch[i]);
------------------------------------------------

Isn't this wrong as i iterates over bytes but but not over (UTF8) characters?

Reply by email

Mattias Gaertner
to Jürgen Hestermann
Show quoted text 13 lines
Am 2014-05-17 10:39, schrieb Mattias Gaertner: > I added a note to the example, that the enumerator is automatically freed by the compiler after the loop. I just had a look at the UTF8 example on http://wiki.freepascal.org/for-in_loop where is says: ------------------------------------------------ for i := 1 to Length(s) do DoSomething(ch[i]); ------------------------------------------------ Isn't this wrong as i iterates over bytes but but not over (UTF8) characters?

Yes.
I replaced the example with a full implementation and some comments.

Mattias

Reply by email