I need to have my PHP extension return an array of objects, but I can't seem to figure out how to do this.
I have a Graph
object written in C++. Graph.getNodes()
returns a std::map<int, Node*>
. Here's the code I have currently:
struct node_object { zend_object std; Node *node; }; zend_class_entry *node_ce;
then
PHP_METHOD(Graph, getNodes) { Graph *graph; GET_GRAPH(graph, obj) // a macro I wrote to populate graph node_object* n; zval* node_zval; if (obj == NULL) { RETURN_NULL(); } if (object_init_ex(node_zval, node_ce) != SUCCESS) { RETURN_NULL(); } std::map nodes = graph->getNodes(); array_init(return_value); for (std::map::iterator i = nodes.begin(); i != nodes.end(); ++i) { php_printf("X"); n = (node_object*) zend_object_store_get_object(node_zval TSRMLS_CC); n->node = i->second; add_index_zval(return_value, i->first, node_zval); } php_printf("]"); }
When i run php -r '$g = new Graph(); $g->getNodes();'
I get the output
XX]Segmentation fault
meaning the getNodes() function loops successfully through my 2-node list, returns, then segfaults. What am I doing wrong?