Use of freed value in iteration - how to workaround?

Discussion in 'Perl Misc' started by A. Farber, Feb 28, 2010.

  1. A. Farber

    A. Farber Guest

    Hello,

    I poll() over TCP-connected clients and sometimes
    have to remove them. The following code works ok
    for me (since many months):

    # add the new client
    $pkg->add($tcpSocket) if $Poll->events($tcpSocket) & POLLIN;

    for my $child (values %Kids) {
    # XXX a child can get removed inbetween
    next unless $child;

    my $fh = $child->{FH};
    my $mask = $Poll->events($fh);

    if ($mask & (POLLERR | POLLHUP)) {
    $child->remove();
    } elsif ($mask & POLLIN) {
    unless ($child->read()) {
    $child->remove();
    next;
    }
    } elsif ($mask & POLLOUT) {
    unless ($child->write()) {
    $child->remove();
    next;
    }
    }
    }

    It probably works ok, because I only remove
    the current element ($child) of the array.

    Now I have new situation, that I sometimes
    have to remove an other element of the array,
    while I'm in the $child->read() method.

    I've added next unless $child; above,
    but still my script dies with the error:
    "Use of freed value in iteration".

    From searching I know, that this is because
    I'm removing an element of an array
    whil iterating over it.

    But my question is, how could I workaround it?

    Thank you
    Alex
     
    A. Farber, Feb 28, 2010
    #1
    1. Advertising

  2. A. Farber

    A. Farber Guest

    On Feb 28, 7:59 pm, Sherm Pendley <>
    wrote:
    > "A. Farber" <> writes:
    > > From searching I know, that this is because
    > > I'm removing an element of an array
    > > whil iterating over it.

    >
    > Instead of removing elements immediately, push them
    > onto another array. Once you're done iterating over
    > the "main" array, iterate over the "hit list" array
    > and remove the elements in it from the "main" array.


    Good idea, thank you!
     
    A. Farber, Feb 28, 2010
    #2
    1. Advertising

  3. A. Farber wrote:
    > Hello,
    >
    > I poll() over TCP-connected clients and sometimes
    > have to remove them. The following code works ok
    > for me (since many months):
    >
    > # add the new client
    > $pkg->add($tcpSocket) if $Poll->events($tcpSocket) & POLLIN;
    >
    > for my $child (values %Kids) {
    > # XXX a child can get removed inbetween
    > next unless $child;


    If the value has been removed from %Kids, $child will not magically turn
    false. (And even if it did, the "next" would occur too late to help).
    I don't understand the rest of what you are doing (your code seems to be
    using objects, but you don't give any indication of what modules they
    are objects of), but perhaps this could help:

    foreach my $key (keys %Kids) {
    next unless exists $Kids{$key};
    my $child=$kids{$key};

    Xho
     
    Xho Jingleheimerschmidt, Feb 28, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dg
    Replies:
    0
    Views:
    332
  2. jimjim
    Replies:
    28
    Views:
    884
    Michael Wojcik
    Apr 14, 2004
  3. David Scarlett

    Just how delicate are freed pointers?

    David Scarlett, May 16, 2004, in forum: C Programming
    Replies:
    73
    Views:
    1,303
    Dan Pop
    May 21, 2004
  4. Rudi
    Replies:
    5
    Views:
    5,145
  5. Stuart Kendrick

    Use of freed value in iteration

    Stuart Kendrick, Aug 5, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    357
    Stuart Kendrick
    Aug 5, 2004
Loading...

Share This Page