|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-09-02 10:35 UTC] tom916 at qq dot com
Description:
------------
<?php
class foo {
private $memcache = null;
public function get_memcache() {
if($this->memcache == null) {
$tmp = new Memcache();
$tmp->addServer('localhost', 11211, 1, 1, 1, 15, true, array(
$this,
'fail'
));
$tmp->setCompressThreshold(8192, 0.2);
$this->memcache = $tmp;
}
return $this->memcache;
}
public function fail($host, $port) {
}
}
/**
* use static $memcache
* result is Segmentation fault
*/
class bar {
private static $memcache = null;
public function get_memcache() {
if(self::$memcache == null) {
$tmp = new Memcache();
$tmp->addServer('localhost', 11211);
$tmp->setCompressThreshold(8192, 0.2);
self::$memcache = $tmp;
}
return self::$memcache;
}
}
/**
* not static ok
class bar {
private $memcache = null;
public function get_memcache() {
if($this->memcache == null) {
$tmp = new Memcache();
$tmp->addServer('localhost', 11211);
$tmp->setCompressThreshold(8192, 0.2);
$this->memcache = $tmp;
}
return $this->memcache;
}
}
*/
$bar = new bar();
$memcache = $bar->get_memcache();
$foo = new foo();
$mem = $foo->get_memcache();
echo "ok\n";
----result is--------
ok
Segmentation fault
------------
not use “ array(
$this,
'fail'
)” param is ok
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 21:00:01 2025 UTC |
hmm, you can simply bypass this issue by define a __destruct to the class like: class { ..... public function __destruct() { $this->memache = NULL; } } will dig this later.use Singleton result is Segmentation fault ----------------------------- <?php class foo { public static function get_instance() { if(! self::$instance) { self::$instance = new foo(); } return self::$instance; } private static $instance; public function get_memcache() { $memcache = new Memcache(); $memcache->addServer("localhost", 11211, 1, 1, 1, 15, true, array( $this, 'fail' )); $this->memcache = $memcache; return $memcache; } public function fail($host, $port) { } } class bar { private static $instance; public static function get_instance() { if(! self::$instance) { self::$instance = new bar(); } return self::$instance; } private $memcache; public function get_memcache() { $this->memcache = new Memcache(); $this->memcache->addServer("localhost", 11211, 1); $this->memcache->setCompressThreshold(8192, 0.2); } //// without this funciton Segmentation fault //public function __destruct(){ // $this->memcache = null; //} } $foo = foo::get_instance()->get_memcache(); $bar = bar::get_instance()->get_memcache(); -------------------------- #0 0x00000000007601f0 in zend_objects_store_del_ref (zobject=0x124a300) at php-5.3.10/Zend/zend_objects_API.c:175 175 GC_ZOBJ_CHECK_POSSIBLE_ROOT(zobject); (gdb) l 170 171 Z_ADDREF_P(zobject); 172 zend_objects_store_del_ref_by_handle_ex(handle, Z_OBJ_HT_P(zobject) TSRMLS_CC); 173 Z_DELREF_P(zobject); 174 175 GC_ZOBJ_CHECK_POSSIBLE_ROOT(zobject); 176 } 177 178 /* 179 * Delete a reference to an objects store entry given the object handle. (gdb)