views:

42

answers:

1

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?

+1  A: 

I simply needed to MAKE_STD_ZVAL(node_zval). A secondary issue with this code was that I was reusing this zval pointer, thus overwriting every previous zval and ending up with an array full of the same object. To remedy this, I initialize node_zval for each loop. Here's the final code:

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();
        }   

        std::map nodes = graph->getNodes();

        array_init(return_value);

        for (std::map::iterator i = nodes.begin(); i != nodes.end(); ++i) {
                MAKE_STD_ZVAL(node_zval);
                if (object_init_ex(node_zval, node_ce) != SUCCESS) {
                        RETURN_NULL();
                }   

                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);
        }
}
John Factorial