@@ -1431,7 +1431,7 @@ static void php_phongo_dispatch_handlers(const char *name, zval *z_event)
14311431#if PHP_VERSION_ID >= 70000
14321432 zval * value ;
14331433
1434- ZEND_HASH_FOREACH_VAL (& MONGODB_G (subscribers ), value ) {
1434+ ZEND_HASH_FOREACH_VAL (MONGODB_G (subscribers ), value ) {
14351435 /* We can't use the zend_call_method_with_1_params macro here, as it
14361436 * does a sizeof() on the name argument, which does only work with
14371437 * constant names, but not with parameterized ones as it does
@@ -1442,11 +1442,11 @@ static void php_phongo_dispatch_handlers(const char *name, zval *z_event)
14421442 HashPosition pos ;
14431443 TSRMLS_FETCH ();
14441444
1445- zend_hash_internal_pointer_reset_ex (& MONGODB_G (subscribers ), & pos );
1446- for (;; zend_hash_move_forward_ex (& MONGODB_G (subscribers ), & pos )) {
1445+ zend_hash_internal_pointer_reset_ex (MONGODB_G (subscribers ), & pos );
1446+ for (;; zend_hash_move_forward_ex (MONGODB_G (subscribers ), & pos )) {
14471447 zval * * value ;
14481448
1449- if (zend_hash_get_current_data_ex (& MONGODB_G (subscribers ), (void * * ) & value , & pos ) == FAILURE ) {
1449+ if (zend_hash_get_current_data_ex (MONGODB_G (subscribers ), (void * * ) & value , & pos ) == FAILURE ) {
14501450 break ;
14511451 }
14521452
@@ -1469,8 +1469,8 @@ static void php_phongo_command_started(const mongoc_apm_command_started_t *event
14691469#endif
14701470 TSRMLS_FETCH ();
14711471
1472- /* Check for subscriber size */
1473- if (zend_hash_num_elements (& MONGODB_G (subscribers )) == 0 ) {
1472+ /* Return early if there are no APM subscribers to notify */
1473+ if (! MONGODB_G ( subscribers ) || zend_hash_num_elements (MONGODB_G (subscribers )) == 0 ) {
14741474 return ;
14751475 }
14761476
@@ -1509,8 +1509,8 @@ static void php_phongo_command_succeeded(const mongoc_apm_command_succeeded_t *e
15091509#endif
15101510 TSRMLS_FETCH ();
15111511
1512- /* Check for subscriber size */
1513- if (zend_hash_num_elements (& MONGODB_G (subscribers )) == 0 ) {
1512+ /* Return early if there are no APM subscribers to notify */
1513+ if (! MONGODB_G ( subscribers ) || zend_hash_num_elements (MONGODB_G (subscribers )) == 0 ) {
15141514 return ;
15151515 }
15161516
@@ -1553,8 +1553,8 @@ static void php_phongo_command_failed(const mongoc_apm_command_failed_t *event)
15531553
15541554 default_exception_ce = zend_exception_get_default (TSRMLS_C );
15551555
1556- /* Check for subscriber size */
1557- if (zend_hash_num_elements (& MONGODB_G (subscribers )) == 0 ) {
1556+ /* Return early if there are no APM subscribers to notify */
1557+ if (! MONGODB_G ( subscribers ) || zend_hash_num_elements (MONGODB_G (subscribers )) == 0 ) {
15581558 return ;
15591559 }
15601560
@@ -2091,8 +2091,12 @@ static void php_phongo_pclient_dtor(void *pp)
20912091/* {{{ PHP_RINIT_FUNCTION */
20922092PHP_RINIT_FUNCTION (mongodb )
20932093{
2094- /* Initialize HashTable for APM subscribers */
2095- zend_hash_init (& MONGODB_G (subscribers ), 0 , NULL , ZVAL_PTR_DTOR , 0 );
2094+ /* Initialize HashTable for APM subscribers, which is initialized to NULL in
2095+ * GINIT and destroyed and reset to NULL in RSHUTDOWN. */
2096+ if (MONGODB_G (subscribers ) == NULL ) {
2097+ ALLOC_HASHTABLE (MONGODB_G (subscribers ));
2098+ zend_hash_init (MONGODB_G (subscribers ), 0 , NULL , ZVAL_PTR_DTOR , 0 );
2099+ }
20962100
20972101 return SUCCESS ;
20982102}
@@ -2272,7 +2276,12 @@ PHP_MSHUTDOWN_FUNCTION(mongodb)
22722276/* {{{ PHP_RSHUTDOWN_FUNCTION */
22732277PHP_RSHUTDOWN_FUNCTION (mongodb )
22742278{
2275- zend_hash_destroy (& MONGODB_G (subscribers ));
2279+ /* Destroy HashTable for APM subscribers, which was initialized in RINIT */
2280+ if (MONGODB_G (subscribers )) {
2281+ zend_hash_destroy (MONGODB_G (subscribers ));
2282+ FREE_HASHTABLE (MONGODB_G (subscribers ));
2283+ MONGODB_G (subscribers ) = NULL ;
2284+ }
22762285
22772286 return SUCCESS ;
22782287}
0 commit comments