properly freeing memory

From: Date: Wed, 16 Sep 1998 07:18:23 +0000
Subject: properly freeing memory
Groups: php.dev 
Request: Send a blank email to php-dev+get-1156@lists.php.net to get a copy of this message
I'm hoping for enlightenment on a quick point of PHP memory management. I've hacked basic_functions.c to add register_shutdown_method(), and to allow it and register_shutdown_function() to accept parameter lists. The point is this: I'm passing the function/object information like this (which might suck but does work :): ------------------------------------ pval param_array; if ( array_init( &param_array ) == FAILURE ) { return; } /* just use type_temp to mark which kind of thing we're registering */ if ( is_method ) { /* registering a method */ convert_to_object( &type_temp ); } else { /* registering a function */ type_temp.type = IS_STRING; } if ( _php3_hash_next_index_pointer_insert(param_array.value.ht, &type_temp )==FAILURE) { return; } for ( i = 0; i < arg_count; i++ ) { if ( _php3_hash_next_index_pointer_insert(param_array.value.ht, params[i] )==FAILURE) { return; } } pval_copy_constructor( &param_array ); if (!user_shutdown_function_names) { user_shutdown_function_names = (HashTable *) emalloc(sizeof(HashTable)); _php3_hash_init(user_shutdown_function_names, 0, NULL, (void (*)(void *))user_shutdown_function_dtor, 0); } if (_php3_hash_next_index_insert(user_shutdown_function_names, &param_array, sizeof(pval), NULL)==FAILURE) { php3_error(E_WARNING, "Couldn't register shutdown function %s()", params[0]->value.str.val ); } ------------------------------------- And I *thought* I was cleaning up properly in user_shutdown_function_dtor() (below), but I guess not, since I keep getting 'Freeing 32 bytes...' messages when I run it (which I don't get when I don't use the hashes). I've been tracing through but I'm missing something. Thanks, Torben ------------------------------------------------------ void user_shutdown_function_dtor(pval *arg) { pval retval; pval **params; int i = 0, param_count; param_count = _php3_hash_num_elements( arg->value.ht ); if ( param_count < 2 ) { /* no function to call */ pval_destructor( arg ); return; } params = (pval **) emalloc(sizeof(pval *) * param_count); for ( i = 0; i < param_count; i++ ) { _php3_hash_index_find( arg->value.ht, i, (void **) &params[i] ); } if ( params[0]->type == IS_OBJECT ) { /* should probably check the # of params again */ if ( call_user_function(&GLOBAL(function_table), params[2], params[1], &retval, param_count - 3, params + 3 )==SUCCESS) { pval_destructor(&retval); } } else { if ( call_user_function(&GLOBAL(function_table), NULL, params[1], &retval, param_count - 2, params + 2 )==SUCCESS) { pval_destructor(&retval); } } pval_destructor( arg ); efree( params ); } -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#1156) next »