From ed36f30eae9efbc0137380ea3c368b9a5d0cf409 Mon Sep 17 00:00:00 2001 From: axel Date: Sat, 21 Oct 2006 20:30:35 +1000 Subject: [PATCH] Fix crash bug in new history code - fish would segfauls in non-interactive mode on exit darcs-hash:20061021103035-ac50b-a2a8d96416ff217cc5640f8f27db8dbac2c39b82.gz --- history.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/history.c b/history.c index 791c2ef83..35899b3ef 100644 --- a/history.c +++ b/history.c @@ -366,7 +366,7 @@ static void item_write( FILE *f, history_mode_t *m, void *v ) /** Release all memory used by the specified history mode. */ -static void history_destroy_mode( wchar_t *name, history_mode_t *m ) +static void history_destroy_mode( history_mode_t *m ) { halloc_free( m->item_context ); @@ -375,11 +375,16 @@ static void history_destroy_mode( wchar_t *name, history_mode_t *m ) munmap( m->mmap_start, m->mmap_length); } +} + +static void history_destroy_mode_wrapper( void *n, history_mode_t *m ) +{ halloc_free( m ); } /** - Create a new empty mode with the specified name + Create a new empty mode with the specified name. + The mode is a halloc context, and the entire mode can be destroyed using halloc_free(). */ static history_mode_t *history_create_mode( const wchar_t *name ) { @@ -395,6 +400,8 @@ static history_mode_t *history_create_mode( const wchar_t *name ) new_mode->save_timestamp=time(0); new_mode->item_context = halloc( 0,0 ); + halloc_register_function( new_mode, (void (*)(void *))&history_destroy_mode, new_mode ); + return new_mode; } @@ -649,7 +656,7 @@ static void history_save_mode( void *n, history_mode_t *m ) free( tmp_name ); } - history_destroy_mode( 0, on_disk); + halloc_free( on_disk); /* if( m->mmap_start && (m->mmap_start != MAP_FAILED ) ) munmap( m->mmap_start, m->mmap_length ); @@ -862,17 +869,20 @@ void history_set_mode( const wchar_t *name ) } void history_init() -{ +{ } void history_destroy() { - hash_foreach( mode_table, (void (*)(void *, void *))&history_save_mode ); - hash_foreach( mode_table, (void (*)(void *, void *))&history_destroy_mode ); - hash_destroy( mode_table ); - free( mode_table ); - mode_table=0; + if( mode_table ) + { + hash_foreach( mode_table, (void (*)(void *, void *))&history_save_mode ); + hash_foreach( mode_table, (void (*)(void *, void *))&history_destroy_mode_wrapper ); + hash_destroy( mode_table ); + free( mode_table ); + mode_table=0; + } }