Script keeps crashing...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SwizzCodes
Forum Newbie
Posts: 20
Joined: Tue Dec 19, 2006 3:23 pm

Script keeps crashing...

Post by SwizzCodes »

Hey all,

I've been a long time lurker here, but this time I really need your help :)
The following script keeps crashing my server, I cannot figure out why, maybe some of you have an idea?

Code: Select all

<?php
class Template
{
    protected $templateFile;
    protected $data;
    
    public function __construct($template, $data)
    {
        $this->data    = $data;
        $this->templateFile = $template;    
    }
    
    public function getData()
    {
        return $this->data;
    }
    
    public function parse($data)
    {
        if(is_array($data) && count($data) > 0)
        {
            ob_start();
            foreach($this->data as $var => $value)
            {
                if(is_a($value, 'Template'))
                {
                    $$var = $this->parse($value->getData());
                }
                else
                {
                    $$var = $value;
                }
            }
            require $this->templateFile;
            $parsed = ob_get_contents();
            ob_end_clean();
            return $parsed;
        }
    }
}
?>
This was the class, now here is how i use it and how it works:

Code: Select all

$test = array("a" => 'abc', "b" => 'bac');
		$tpl = new Template('library/view/html/test.php', $test);
		echo $tpl->parse($tpl->getData());
Now here is how I use it, and it crashes my server:

Code: Select all

$test2 = array('bla' => "hey", "what" => "frage");
		$test = array("a" => 'abc', "b" => 'bac', "c" => new Template('library/view/html/test2.php', $test2));
		$tpl = new Template('library/view/html/test.php', $test);
		echo $tpl->parse($tpl->getData());
And I've also got an error log, if that is usefull:

Code: Select all

Thread 0 Crashed:
0   libphp5.so 	0x0248b458 zend_mm_add_to_free_list + 208
1   libphp5.so 	0x0248c824 _zend_mm_free_int + 1168
2   libphp5.so 	0x0248d308 _efree + 56
3   libphp5.so 	0x02461c54 php_end_ob_buffer + 1928
4   libphp5.so 	0x02461d60 php_end_ob_buffers + 60
5   libphp5.so 	0x0244a28c php_request_shutdown + 348
6   libphp5.so 	0x0254fd4c php_apache_request_dtor + 32
7   libphp5.so 	0x025504e0 php_handler + 1680
8   httpd      	0x0000ac74 ap_run_handler + 100 (config.c:157)
9   httpd      	0x0000b2e8 ap_invoke_handler + 248 (config.c:373)
10  httpd      	0x00031c9c ap_process_request + 108 (http_request.c:258)
11  httpd      	0x00031278 ap_process_http_connection + 120 (http_core.c:184)
12  httpd      	0x0001efd4 ap_run_process_connection + 100 (connection.c:43)
13  httpd      	0x0004f58c child_main + 1276 (prefork.c:641)
14  httpd      	0x0004f750 make_child + 320 (prefork.c:739)
15  httpd      	0x0004fed8 ap_mpm_run + 1672 (prefork.c:1036)
16  httpd      	0x0000362c main + 3324 (main.c:717)
17  httpd      	0x0000219c _start + 760
18  httpd      	0x00001ea0 start + 48
Any input is greatly appreciated!

Thx!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what about the apache error log?
SwizzCodes
Forum Newbie
Posts: 20
Joined: Tue Dec 19, 2006 3:23 pm

Post by SwizzCodes »

Code: Select all

[Tue Dec 19 22:12:20 2006] [error] [client ::1] PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 40961 bytes) in /usr/local/apache2/htdocs/GreenBoard/library/framework/Template.php on line 20
[Tue Dec 19 22:12:23 2006] [notice] child pid 2618 exit signal Segmentation fault (11)
[Tue Dec 19 22:13:34 2006] [error] [client 127.0.0.1] PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 40961 bytes) in /usr/local/apache2/htdocs/GreenBoard/library/framework/Template.php on line 20
[Tue Dec 19 22:13:37 2006] [notice] child pid 2616 exit signal Segmentation fault (11)
seems pretty wierd? How can my memory be filled up so fast? Is there an mistake in my recursion?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It's just a thought, but I would suspect a loop issue somewhere, as these have a tendency to be the culprit on most execution time and memory issues.
SwizzCodes
Forum Newbie
Posts: 20
Joined: Tue Dec 19, 2006 3:23 pm

Post by SwizzCodes »

hmm .. interessting is: if i leave the ob_start() and co. away, it's not crashing anymore. Maybe has something to do with the buffer and stuff inside the recursion ( multiple ob_get_contents() inside each other)..

but that would be <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>, because it's pretty necessary here ^^
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

The crash is almost certainly something to do with the recursion. Crashes usually mean unending loops but I'm not convinced that is the case here. I'm concerned about the double dollar; you are risking the possibility of one of the array keys being all 'this' and breaking your code. otherwise extract might be interesting to you.

Other thing you might want to consider is Zend View or at least looking at the source code.
SwizzCodes
Forum Newbie
Posts: 20
Joined: Tue Dec 19, 2006 3:23 pm

Post by SwizzCodes »

Zend View is kind of doing the same thing as I do, but it doesnt do it with recursion. Too bad that was my initial idea, having a rendertree-like object which I can render when everything else is done. That would make it unnecessary for every layer to render it's view and pass it to it's parent. hmlgrml :?

any further ideas?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Zend View is kind of doing the same thing as I do, but it doesnt do it with recursion.
That and the fact that you are reinventing the wheel is why I mentioned it.
SwizzCodes
Forum Newbie
Posts: 20
Joined: Tue Dec 19, 2006 3:23 pm

Post by SwizzCodes »

I'm just trying to put some nice rims on the wheel, but they don't seem to work very well, hu? ;)
Post Reply