Re: Introduction + Using another extension's resources
| From: | Johannes Schlüter | Date: | Sat, 17 Jan 2009 10:52:38 +0000 |
| Subject: | Re: Introduction + Using another extension's resources | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-6058@lists.php.net to get a copy of this message | ||
On Sat, 2009-01-17 at 01:58 -0800, weedguy wrote:
> ZEND_FUNCTION(test_func)
> {
> int argc = ZEND_NUM_ARGS();
>
> if (1 != argc)
> WRONG_PARAM_COUNT;
That's not needed, zend_parse_parameters will give a nicer error
message.
> zval *mysql_res = NULL;
> php_mysql_conn *mysql = NULL;
>
> int res_type = -1;
>
> if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc
> TSRMLS_CC, "r", &mysql_res))
Why quiet? Quiet should be used when ou'll ry another way to fetch the
params in case this fails.
> RETURN_NULL();
>
> mysql = (php_mysql_conn *) zend_list_find(Z_RESVAL_P(mybs_res),
> &res_type);
You're not checking whether that's really a mysql resource, doing
test_func(fopen($file,"r")) will certainly segfault therefore.
But this can't be fixed as ext/mysql doesn't export le_link.
> mysql_close(&mysql->conn);
> }
>
> Now, everything works fine until I pass mysql->conn to mysql_close. At this
> point, I get a seg fault whenever I try to call the above function in my PHP
> script. The argument I pass to this function is the resource returned by
> calling the php mysql_connect() function. If I get a MySQL reference via
Does your debugger show you, you got a proper resource? What does the
backtrace look like?
And closing in that way is dangerous, too: You have to destroy the
resource, too, else PHP will try to close the connection itself lateron.
> I've also checked the forums, and there was a mention of how to access
> another extension's resources, either statically or by using the
> ZEND_MOD_REQUIRED macro. I'm not to keen on static linking, and I'm not
> entirely sure on what to do exactly after specifying
> ZEND_MOD_REQUIRED("mysql") as a dependency (I checked the pdo_sqlite
> extension source, not much help from there).
That's for making sure the other extension was loaded when your
extension is being loaded. For a test this isn't needed. See the pdo
drivers for examples of extensions depending on pdo core.
johannes