SPL Examples Broken?

php.internals

Adam Maccabee Trachtenberg

22 years ago
I'm trying to run the SPL examples and running into some problems. Some of these I was able to work around, but I eventually end up generating a 192 Mb core dump with a 1000+ line gdb backtrace. It's actually quite an impressive crash. :) First of all, I'm running under PHP 5 Beta 3 since the current CVS no longer allows an Interface to implement an Interface. (Maybe this is the biggest problem? I thought I rememebered Andi claim that Zeev will be fixing this RSN?) Second, there is a small typo in recursiveiteratoriterator.inc. Line 67 has one extra closing ')'. I fixed this. Next, I get an error that recursivedirectoryiterator.inc cannot be found. Based on some guess work, I created this file and placed the following PHP code inside: class RecursiveDirectoryIterator extends DirectoryTreeIterator { } I'm not sure if this is correct, but it allowed me to run the following without any warnings: ~/Documents/php/php-5.0.0b3/sapi/cli/php -d include_path=. -d auto_prepend_file=autoload.inc tree.php ../../dba Unfortunately, this gives a big core dump. :( If anyone has some suggestions I'm happy to try them. In the meantime I will start with a more basic example and see what pushes PHP over the edge. Maybe that will give a more reasonable backtrace. (Marcus: I will send you my current backtrace if you think it will be helpful.) -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Marcus Börger

22 years ago
Hello Adam, Friday, January 23, 2004, 9:48:42 PM, you wrote:
> I'm trying to run the SPL examples and running into some > problems. Some of these I was able to work around, but I eventually > end up generating a 192 Mb core dump with a 1000+ line gdb > backtrace. It's actually quite an impressive crash. :)
> First of all, I'm running under PHP 5 Beta 3 since the current CVS no > longer allows an Interface to implement an Interface. (Maybe this is > the biggest problem? I thought I rememebered Andi claim that Zeev will > be fixing this RSN?)
> Second, there is a small typo in recursiveiteratoriterator.inc. Line > 67 has one extra closing ')'. I fixed this.
> Next, I get an error that recursivedirectoryiterator.inc cannot be > found. Based on some guess work, I created this file and placed the > following PHP code inside:
> class RecursiveDirectoryIterator extends DirectoryTreeIterator { }
> I'm not sure if this is correct, but it allowed me to run the > following without any warnings:
> ~/Documents/php/php-5.0.0b3/sapi/cli/php -d include_path=. > -d auto_prepend_file=autoload.inc tree.php ../../dba
> Unfortunately, this gives a big core dump. :(
> If anyone has some suggestions I'm happy to try them. In the meantime > I will start with a more basic example and see what pushes PHP over > the edge. Maybe that will give a more reasonable backtrace. (Marcus: I > will send you my current backtrace if you think it will be helpful.)
The way you created RecursiveDirectoryIterator should be the problem. At least i hope so. The class is suppoed to implement interface RecursiveIterator which you are obviously missing. Apart from that the class is a built in class (or are you trying the code without ext/spl?). You can verify the existence of the class by the following line: $ php -r 'Reflection_Class::export("RecursiveDirectoryIterator");' You should then see an outut like this: Class [ <internal> <iterateable> class RecursiveDirectoryIterator extends DirectoryIterator implements Traversable, Iterator, RecursiveIterator ] { - Constants [0] { } [...] Where two things are important. First the fact that the class is an internal class and second that it implements interface RecursiveIterator. You may also send me the bt. But i know those funny bitches. Such lengthy bts occur when the error happens very deep inrecursion. And thanks for noticing the error in the example (i shouldn't rewrite test files when no longer using them). Best regards, Marcus mailto:helly@php.net

Adam Maccabee Trachtenberg

22 years ago
On Fri, 23 Jan 2004, Marcus Boerger wrote:
> The way you created RecursiveDirectoryIterator should be the problem. > At least i hope so. The class is suppoed to implement interface > RecursiveIterator which you are obviously missing. Apart from that the class > is a built in class (or are you trying the code without ext/spl?). > You can verify the existence of the class by the following line:
I am an idiot. I forgot that I did not build PHP Beta 3 with SPL, so when I switched from my "current CVS" build to work around the "implements" bug, I eliminated SPL from my copy of PHP. :) I will try rebuilding things and take a second look. Thanks. -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Adam Maccabee Trachtenberg

22 years ago
On Fri, 23 Jan 2004, Adam Maccabee Trachtenberg wrote:
> I will try rebuilding things and take a second look.
The tree.php example now works, but I am getting errors with findfile.php: [adam@benlev:~/CVS/php/php-src/ext/spl/examples] $ ../../../sapi/cli/php -d auto_prepend_file=autoload.inc findfile.php . findfile.php PHP Notice: Undefined property: FindFile::$it in /usr/local/cvs/php/php-src/ext/spl/examples/findfile.php on line 36 PHP Fatal error: Call to a member function current() on a non-object in /usr/local/cvs/php/php-src/ext/spl/examples/findfile.php on line 36 /Users/adam/CVS/php/php-src/main/streams/streams.c(371) : Stream of type 'dir' 0xac0e80 (path:(null)) was not closed -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Marcus Börger

22 years ago
Hello Adam, Friday, January 23, 2004, 11:18:09 PM, you wrote:
> On Fri, 23 Jan 2004, Adam Maccabee Trachtenberg wrote:
>> I will try rebuilding things and take a second look.
> The tree.php example now works, but I am getting errors with > findfile.php:
> [adam@benlev:~/CVS/php/php-src/ext/spl/examples] $ > ../../../sapi/cli/php -d auto_prepend_file=autoload.inc findfile.php > . findfile.php > PHP Notice: Undefined property: FindFile::$it in > /usr/local/cvs/php/php-src/ext/spl/examples/findfile.php on line 36 > PHP Fatal error: Call to a member function current() on a non-object > in /usr/local/cvs/php/php-src/ext/spl/examples/findfile.php on line 36 > /Users/adam/CVS/php/php-src/main/streams/streams.c(371) : Stream of > type 'dir' 0xac0e80 (path:(null)) was not closed
Fixed too. Hey if you find some cool & easy examples please tell me. Lukas already mentioned some random iterator. That would require an interface that add a count() method to the Seekable interface. And an iterator class that randomly seeks and reads from such interfaces. Of course the class would need to build up an array of available indices and only return elements not read so far by dropping any read index from the available list when read. Best regards, Marcus mailto:helly@php.net

Adam Maccabee Trachtenberg

22 years ago
On Fri, 23 Jan 2004, Marcus Boerger wrote:
> Fixed too. Hey if you find some cool & easy examples please tell me.
Thanks for the fix. I'll let you know when I come up with something cool. :)
> Lukas already mentioned some random iterator. That would require an > interface that add a count() method to the Seekable interface. And > an iterator class that randomly seeks and reads from such interfaces. > Of course the class would need to build up an array of available indices > and only return elements not read so far by dropping any read index from > the available list when read.
This is definitely nice. A reverse iterator could also be useful. Right now, I am having problems combining FilterIterator with ArrayIterator. (I am just exploring how everything works and starting with basic examples.) I want to do something like: class ArrayFilterIterator extends FilterIterator { protected $n; function __construct($a, $n) { parent::__construct(new ArrayIterator($a)); $this->n = $n; } function accept() { return $this->current() >= $this->n; } } $a = array('a' => 1, 'b' => 2, 'c' => 3); foreach (new ArrayFilterIterator($a) as $k => $v) { print "$k: $v\n"; } But ArrayIterator has a private constructor, so that doesn't work. I tried passing an ArrayObject, but then I get a message that I need to pass an Iterator. -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Adam Maccabee Trachtenberg

22 years ago
On Sat, 24 Jan 2004, Adam Maccabee Trachtenberg wrote:
> On Fri, 23 Jan 2004, Marcus Boerger wrote: > > > Fixed too. Hey if you find some cool & easy examples please tell me. > > Thanks for the fix. I'll let you know when I come up with something > cool. :)
I have another example for you to check out. This one works correctly, but leaks memory. Assume this is setup like findfile.php: class RegexFindFile extends FindFile { function accept() { return preg_match($this->file, $this->current()); } } foreach(new RegexFindFile($argv[1], $argv[2]) as $pathname => $file) echo $file->getPathname()."\n"; [adam@benlev:~/CVS/php/php-src/ext/spl/examples] $ php ./findfile.php . '/php$/' ./dba_array.php ./dba_dump.php ./directorytree.php ./findfile.php ./ini_groups.php ./tree.php /Users/adam/CVS/php/php-src/main/streams/streams.c(371) : Stream of type 'dir' 0xac81d0 (path:(null)) was not closed /Users/adam/CVS/php/php-src/main/streams/streams.c(371) : Stream of type 'dir' 0xac74a8 (path:(null)) was not closed /Users/adam/CVS/php/php-src/ext/spl/spl_directory.c(125) : Freeing 0x00AC7838 (6 bytes), script=./findfile.php Last leak repeated 1 time /Users/adam/CVS/php/php-src/main/streams/streams.c(215) : Freeing 0x00AC81D0 (132 bytes), script=./findfile.php /Users/adam/CVS/php/php-src/main/streams/plain_wrapper.c(872) : Actual location (location was relayed) Last leak repeated 1 time /Users/adam/CVS/php/php-src/ext/spl/spl_directory.c(93) : Freeing 0x00AC7D70 (1056 bytes), script=./findfile.php Last leak repeated 1 time /Users/adam/CVS/php/php-src/ext/spl/spl_directory.c(99) : Freeing 0x00AC7780 (32 bytes), script=./findfile.php /Users/adam/CVS/php/php-src/Zend/zend_hash.c(157) : Actual location (location was relayed) Last leak repeated 1 time /Users/adam/CVS/php/php-src/ext/spl/spl_directory.c(98) : Freeing 0x00AC7710 (44 bytes), script=./findfile.php Last leak repeated 1 time === Total 10 memory leaks detected === -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!