PHP中的分段错误?

时间:2011-12-17 02:40:56

标签: php segmentation-fault mysqli

为什么以下代码会导致分段错误?

<?php

$CNX = new mysqli('localhost','dbuser', 'dbpass', 'dbtest');

class DAO
{
        var $stmt;

        function DAO()
        {
                $this->stmt = $GLOBALS['CNX']->stmt_init();
        }

        function listing()
        {
                $this->stmt->prepare('SELECT * FROM any_table');
        }
}

class Page
{
        function go()
        {
                $d1 = new DAO();
                $d2 = new DAO();
                $d1->listing();
                exit;
        }
}

$tpl = new Page();
$tpl->go();
?>

附加说明

1)我在两个不同的LAMP服务器上运行此代码,它们都在我的error.log文件中生成child pid somenumber exit signal Segmentation fault (11)之类的错误。在一台服务器上,我正在运行Ubuntu 10.04,Apache 2,Mysql 5和PHP 5.3.2-1ubuntu4.9,而在另一台服务器上我运行的是Ubuntu 11.10,Apache 2,mysql 5和PHP 5.3.6-13ubuntu3.3 。

2)这很奇怪,但当我注释掉$d2 = new DAO();行或exit;行时,分段错误消失了。

3)无论我在准备好的语句

中选择哪个db表,都会发生分段错误

发生了什么事?我很沮丧。由于这种分段错误,我已经失去了一整天的时间。请...如果有人知道发生了什么,请告诉我

由于

2 个答案:

答案 0 :(得分:7)

看起来你在mysqli模块中发现了一个错误!

将其提交至https://bugs.php.net/

但是,不要指望明天修复它。找到一种方法来解决这个问题,或者尝试使用不同的PHP / mysqli版本来查看是否可以使用。

作为参考,我也可以重现它,这里是一个回溯。在尝试关闭连接并清理时,看起来事物正在爆炸:

jon@jonx:~/tmp$ gdb `which php`
...
Reading symbols from /usr/bin/php...(no debugging symbols found)...done.
(gdb) run tmp.php
Starting program: /usr/bin/php tmp.php
[Thread debugging using libthread_db enabled]
[New Thread 0x7ffff3cb7700 (LWP 1028)]
[Thread 0x7ffff3cb7700 (LWP 1028) exited]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff458ca25 in vio_peer_addr () from /usr/lib/libmysqlclient_r.so.16
(gdb) bt
#0  0x00007ffff458ca25 in vio_peer_addr () from /usr/lib/libmysqlclient_r.so.16
#1  0x00007ffff458d873 in net_real_write () from /usr/lib/libmysqlclient_r.so.16
#2  0x00007ffff458dd5b in net_flush () from /usr/lib/libmysqlclient_r.so.16
#3  0x00007ffff458df40 in net_write_command () from /usr/lib/libmysqlclient_r.so.16
#4  0x00007ffff458aa84 in cli_advanced_command () from /usr/lib/libmysqlclient_r.so.16
#5  0x00007ffff455c23c in mysql_stmt_close () from /usr/lib/libmysqlclient_r.so.16
#6  0x00007ffff42f3815 in ?? () from /usr/lib/php5/20090626/mysqli.so
#7  0x00007ffff42f38ca in ?? () from /usr/lib/php5/20090626/mysqli.so
#8  0x00000000006b6ad1 in zend_objects_store_del_ref_by_handle_ex ()
#9  0x00000000006b6af3 in zend_objects_store_del_ref ()
#10 0x0000000000683662 in _zval_ptr_dtor ()
#11 0x000000000069f313 in zend_hash_destroy ()
#12 0x00000000006b2ae9 in zend_object_std_dtor ()
#13 0x00000000006b2b09 in zend_objects_free_object_storage ()
#14 0x00000000006b663f in zend_objects_store_free_object_storage ()
#15 0x0000000000683c84 in ?? ()
#16 0x0000000000691cd5 in ?? ()
#17 0x000000000063eccf in php_request_shutdown ()
#18 0x000000000072c76c in ?? ()
#19 0x00007ffff55f5eff in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#20 0x0000000000428859 in _start ()

答案 1 :(得分:0)

我刚刚在我的Linux机器上启用了xdebugger。然后我在PHPStorm中调试了代码,它给了我导致错误的确切代码行。

错误是由于在我的情况下递归调用函数,例如:

public function1()
{
    $this->function2();
}

public function2()
{
    $this->function1();
}

我希望这会对你有所帮助。

谢谢,

Dipti