A
A. Farber
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
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