zephir 引用计数问题,查了非常久, 最后发现是 PHP 7.2 zend_hash.c中对数组增加了一个 被引用数的判断,被引用数大于1的不可以修改,需要先分离,最后是官方修改了zephir编译器,解决了
//fight.zep
namespace Test;
class Fight
{
public heros_attr = [];
public function __construct(var ids) {
var id;
for id in ids {
var heroAttr;
let heroAttr = new HeroAttr(id);
let this->heros_attr["P1"][] = heroAttr;
}
for id in ids {
var heroAttr;
let heroAttr = new HeroAttr(id);
let this->heros_attr["P2"][] = heroAttr;
}
}
public function get_fight_ret()
{
var p = "P1", key = 0;
this->add_buff(p, key);
this->add_buff(p, key);
this->add_buff(p, key);
}
public function add_buff(var p, var key) {
var attached_buffs = [];
let attached_buffs = this->heros_attr[p][key]->get_my_buffs(); //注释这里就OK
this->heros_attr[p][key]->add_my_buff(33);
}
}
//heroattr.zep
namespace Test;
class HeroAttr
{
public id;
public my_buffs = [];
public function __construct(int id) {
let this->id = id;
}
public function add_my_buff(var buff) {
let this->my_buffs[] = buff;
}
public function get_my_buffs() {
return this->my_buffs;
}
}
<?php
//test.php
$fight_obj = new Test\Fight([33, 44, 55]);
$ret = $fight_obj->get_fight_ret();
echo "end\n";
[root@game test]# zephir build
Compiling...
Installing...
Extension installed!
Don't forget to restart your web server
You have mail in /var/spool/mail/root
[root@game test]# php test.php
php: /root/soft/php7/php-7.2.10/Zend/zend_hash.c:712: _zend_hash_index_add_or_update_i: Assertion `((ht)->gc.refcount == 1) || ((ht)->u.flags & (1<<6))' failed.
Aborted