Changeset 6654 for trunk/ARBDB/arbdb.cxx

Show
Ignore:
Timestamp:
13/05/10 17:30:03 (2 years ago)
Author:
westram
Message:
  • This patch stuffs all memory leaks in ARBDB (as far as covered by unit-tests) for DEBUG and NDEBUG version!
  • ARBDB global data (aka 'gb_local')
    • track number of opened/closed DBs
    • GBK_install_SIGSEGV_handler only once in GB_init_gb()
    • when closing last DB -> call GB_exit_gb() freeing more memory (also called atexit, including assertion failing if a DB is still open)
    • added class ARBDB_memory_manager (just calls gbm_init_mem + gbm_flush_mem)
    • GB_exit_gb flushes ARBDB_memory_manager (if all blocks were freed by gbm_free_mem, everything will be flushed)
  • fixed a few more static buffer leaks
  • gb_make_entry - alloc correct blocksizes for GB_STRING and GB_LINK
  • fixed a hack in gb_read_bin_rek_V2
    • was increasing size of memory block to avoid illegal memory access in decompress (introduced in [1003]; may occur again)
    • due to mismatch in size between gbm_get_mem and gbm_free_mem memory was never flushed even if all blocks have been freed
  • fixed memleak in gb_read_bin_rek_V2
    • when loading mapfile, previously created 'gb_main' was not freed
  • added allocation logger to admalloc.cxx (inactive - use TRACE_ALLOCS to activate it)
    • traces all allocs/frees of ARBDB managed memory
    • detects wrong usage (double free, size and index mismatch) and shows were the block was allocated
    • added new backtrace functions helpful for debugging (store stack-backtraces and print them later)
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/ARBDB/arbdb.cxx

    r6648 r6654  
    285285}; 
    286286 
     287void GB_exit_gb() { 
     288    if (gb_local) { 
     289        gb_assert(gb_local->openedDBs == gb_local->closedDBs); 
     290 
     291        free(gb_local->bitcompress); 
     292        gb_free_compress_tree(gb_local->bituncompress); 
     293        free(gb_local->write_buffer); 
     294 
     295        free(check_out_buffer(&gb_local->buf2)); 
     296        free(check_out_buffer(&gb_local->buf1)); 
     297 
     298        gbm_free_mem((char*)gb_local, sizeof(*gb_local), 0); 
     299        gb_local = NULL; 
     300 
     301        gbm_flush_mem(); 
     302    } 
     303} 
     304 
    287305void GB_init_gb() { 
    288306    if (!gb_local) { 
    289         gb_local = (struct gb_local_data *)gbm_get_mem(sizeof(struct gb_local_data), 0); 
     307        GBK_install_SIGSEGV_handler(true);          // never uninstalled 
     308 
     309        gbm_init_mem(); 
     310 
     311        gb_local = (gb_local_data *)gbm_get_mem(sizeof(gb_local_data), 0); 
    290312 
    291313        init_buffer(&gb_local->buf1, 4000); 
     
    300322        gb_local->bitcompress   = gb_build_compress_list(GB_BIT_compress_data, 1, &(gb_local->bc_size)); 
    301323 
     324        gb_local->openedDBs = 0; 
     325        gb_local->closedDBs = 0; 
     326 
    302327#ifdef ARBDB_SIZEDEBUG 
    303328        arbdb_stat = (long *)GB_calloc(sizeof(long), 1000); 
    304329#endif 
     330 
     331        atexit(GB_exit_gb); 
    305332    } 
    306333} 
     
    969996{ 
    970997    char *d; 
    971     long memsize[2]; 
     998    long memsize; 
    972999 
    9731000    GB_TEST_WRITE(gbd, GB_BITS, "GB_write_bits"); 
     
    9751002    gb_save_extern_data_in_ts(gbd); 
    9761003 
    977     d = gb_compress_bits(bits, size, (const unsigned char *)c_0, memsize); 
     1004    d = gb_compress_bits(bits, size, (const unsigned char *)c_0, &memsize); 
    9781005    gbd->flags.compressed_data = 1; 
    979     GB_SETSMDMALLOC(gbd, size, memsize[0], d); 
     1006    GB_SETSMDMALLOC(gbd, size, memsize, d); 
    9801007    gb_touch_entry(gbd, GB_NORMAL_CHANGE); 
    9811008    GB_DO_CALLBACKS(gbd); 
     
    25992626 
    26002627GB_ERROR GB_print_debug_information(void */*dummy_AW_root*/, GBDATA *gb_main) { 
    2601     int i; 
    26022628    GB_MAIN_TYPE *Main = GB_MAIN(gb_main); 
    26032629    GB_push_transaction(gb_main); 
    2604     for (i=0; i<Main->keycnt; i++) { 
     2630    for (int i=0; i<Main->keycnt; i++) { 
    26052631        if (Main->keys[i].key) { 
    26062632            printf("%3i %20s    nref %i\n", i, Main->keys[i].key, (int)Main->keys[i].nref); 
     
    26102636        } 
    26112637    } 
    2612     gbm_debug_mem(Main); 
     2638    gbm_debug_mem(); 
    26132639    GB_pop_transaction(gb_main); 
    26142640    return 0;