Jump to content

Feeding MultipleIterator a generator


NotionCommotion

Recommended Posts

The following script causes a segfault during the second $a2->test() pass.  I then disabled my debugger (phped) and it works as desired.  If I replace the generator with an ArrayIterator, I don't get the errors even with the debugger enabled.  Is there something wrong with my script or is there something wrong with the debugger?  Thanks

<?php
ini_set('display_errors', 1);
class A
{
    public function test()
    {
        $iterator = new \MultipleIterator(\MultipleIterator::MIT_NEED_ALL|\MultipleIterator::MIT_KEYS_ASSOC);
        $iterator1 = $this->getIterator(1);
        $iterator2 = $this->getIterator(2);
        $iterator->attachIterator($iterator1, 'iterator1');
        $iterator->attachIterator($iterator2, 'iterator2');
        $values = [];
        foreach($iterator as $v) {
            $values[] = $v['iterator2'] - $v['iterator1'];
        }
        return $values;
    }

    public function getIterator($x)
    {
        for($index=0; $index < 10; $index++) {
            yield $x*$index; 
        }
    }
}

$a1 = new A();
$o1=$a1->test();
print_r($o1);

$a2 = new A();
$o2=$a2->test();
print_r($o2);

 

Link to comment
Share on other sites

14 hours ago, requinix said:

Do you have any watches or conditions set up with the debugger? Exactly when/where does it segfault?

No watches/conditions.  For the script I posted, it happens the second time $iterator = new \MultipleIterator(\MultipleIterator::MIT_NEED_ALL|\MultipleIterator::MIT_KEYS_ASSOC); is called.  But for the below script, it happens on the second generator on the foreach($iterator as $v) line.


 

<?php
ini_set('display_errors', 1);
class A
{
    public function test($iterator1, $iterator2)
    {
        $iterator = new \MultipleIterator(\MultipleIterator::MIT_NEED_ALL|\MultipleIterator::MIT_KEYS_ASSOC);
        $iterator->attachIterator($iterator1, 'iterator1');
        $iterator->attachIterator($iterator2, 'iterator2');
        $values = [];
        foreach($iterator as $v) {
            $values[] = $v['iterator2'] - $v['iterator1'];
        }
        return $values;
    }

    public function getArrayIterator($x)
    {
        $a=[];
        foreach([0,1,2,3,4] as $i) {
            $a[]=$x * $i;
        }
        return new ArrayIterator($a);;
    }
    
    public function getGeneratorIterator($x)
    {
        for($i=0; $i < 5; $i++) {
            yield $x*$i; 
        }
    }
}

$a1 = new A();
$a2 = new A();

$o=$a1->test($a1->getArrayIterator(1), $a1->getArrayIterator(2));
print_r($o);
$o=$a1->test($a1->getArrayIterator(3), $a1->getArrayIterator(4));
print_r($o);

$o=$a2->test($a2->getArrayIterator(1), $a2->getArrayIterator(2));
print_r($o);
$o=$a2->test($a2->getArrayIterator(3), $a2->getArrayIterator(4));
print_r($o);

$o=$a1->test($a1->getGeneratorIterator(1), $a1->getGeneratorIterator(2));
print_r($o);

//Script gets where without segfault

$o=$a2->test($a2->getGeneratorIterator(1), $a2->getGeneratorIterator(2));
print_r($o);

 

Link to comment
Share on other sites

Okay... Does the debugger itself crash? Or does PHP crash and the debugger handles it gracefully? Hopefully PHP.

Try this. Set a breakpoint on the line where it crashes and run the debugger. When it stops, grab the PID of PHP and hook into it with gdb. Then continue running and see if gdb can catch the problem. Then get a backtrace.

Link to comment
Share on other sites

2 hours ago, requinix said:

Okay... Does the debugger itself crash? Or does PHP crash and the debugger handles it gracefully? Hopefully PHP.

Apr 28 15:00:51 stage kernel: php-fpm[29337]: segfault at 60600000001 ip 00007fd51efcd270 sp 00007ffc86605580 error 6 in dbg-php-7.0.so[7fd51efb5000+2c000]

Based on the above, it was the debugger that crashed and not PHP, true?  New debugger firmware cured the issue.  Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.