Changeset 815


Ignore:
Timestamp:
Nov 21, 2006 12:39:31 PM (17 years ago)
Author:
bennylp
Message:

Fixed handles leak upon program exit, by introducing pj_shutdown() and pj_atexit(). Also fixed handle leaks in SIP transaction layer and SIP endpoint.

Location:
pjproject/trunk
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/include/pj/types.h

    r764 r815  
    292292 * in random string generation, and to initialize operating system dependent 
    293293 * functionality (such as WSAStartup() in Windows). 
     294 * 
     295 * @return PJ_SUCCESS on success. 
    294296 */ 
    295297PJ_DECL(pj_status_t) pj_init(void); 
     298 
     299 
     300/** 
     301 * Shutdown PJLIB. 
     302 */ 
     303PJ_DECL(void) pj_shutdown(void); 
     304 
     305/** 
     306 * Register cleanup function to be called by PJLIB when pj_shutdown() is  
     307 * called. 
     308 * 
     309 * @param func      The function to be registered. 
     310 * 
     311 * @return PJ_SUCCESS on success. 
     312 */ 
     313PJ_DECL(pj_status_t) pj_atexit(void (*func)(void)); 
     314 
     315 
    296316 
    297317/** 
  • pjproject/trunk/pjlib/src/pj/except.c

    r635 r815  
    5252} 
    5353 
     54static void exception_cleanup(void) 
     55{ 
     56    if (thread_local_id != -1) { 
     57        pj_thread_local_free(thread_local_id); 
     58        thread_local_id = -1; 
     59    } 
     60} 
     61 
    5462PJ_DEF(void) pj_push_exception_handler_(struct pj_exception_state_t *rec) 
    5563{ 
     
    5967        pj_thread_local_alloc(&thread_local_id); 
    6068        pj_assert(thread_local_id != -1); 
     69        pj_atexit(&exception_cleanup); 
    6170    } 
    6271    parent_handler = pj_thread_local_get(thread_local_id); 
  • pjproject/trunk/pjlib/src/pj/os_core_unix.c

    r750 r815  
    148148    pj_generate_unique_string( &guid ); 
    149149 
    150     /* Initialize exception ID for the pool.  
    151      * Must do so after critical section is configured. 
    152      */ 
    153     rc = pj_exception_id_alloc("PJLIB/No memory", &PJ_NO_MEMORY_EXCEPTION); 
    154     if (rc != PJ_SUCCESS) 
    155         return rc; 
    156  
    157150    /* Startup timestamp */ 
    158151#if defined(PJ_HAS_HIGH_RES_TIMER) && PJ_HAS_HIGH_RES_TIMER != 0 
     
    170163    return PJ_SUCCESS; 
    171164} 
     165 
     166/* 
     167 * pj_atexit() 
     168 */ 
     169PJ_DEF(pj_status_t) pj_atexit(void (*func)(void)) 
     170{ 
     171    if (atexit_count >= PJ_ARRAY_SIZE(atexit_func)) 
     172        return PJ_ETOOMANY; 
     173 
     174    atexit_func[atexit_count++] = func; 
     175    return PJ_SUCCESS; 
     176} 
     177 
     178/* 
     179 * pj_shutdown(void) 
     180 */ 
     181PJ_DEF(void) pj_shutdown() 
     182{ 
     183    int i; 
     184 
     185    /* Call atexit() functions */ 
     186    for (i=atexit_count-1; i>=0; --i) { 
     187        (*atexit_func[i])(); 
     188    } 
     189    atexit_count = 0; 
     190 
     191    /* Free exception ID */ 
     192    if (PJ_NO_MEMORY_EXCEPTION != -1) { 
     193        pj_exception_id_free(PJ_NO_MEMORY_EXCEPTION); 
     194        PJ_NO_MEMORY_EXCEPTION = -1; 
     195    } 
     196 
     197#if PJ_HAS_THREADS 
     198    /* Destroy PJLIB critical section */ 
     199    pj_mutex_destroy(&critical_section); 
     200 
     201    /* Free PJLIB TLS */ 
     202    if (thread_tls_id != -1) { 
     203        pj_thread_local_free(thread_tls_id); 
     204        thread_tls_id = -1; 
     205    } 
     206#endif 
     207} 
     208 
    172209 
    173210/* 
  • pjproject/trunk/pjlib/src/pj/os_core_win32.c

    r746 r815  
    109109 */ 
    110110static pj_thread_desc main_thread; 
    111 static long thread_tls_id; 
     111static long thread_tls_id = -1; 
    112112static pj_mutex_t critical_section_mutex; 
    113  
     113static unsigned atexit_count; 
     114static void (*atexit_func[32])(void); 
    114115 
    115116/* 
     
    178179 
    179180/* 
     181 * pj_atexit() 
     182 */ 
     183PJ_DEF(pj_status_t) pj_atexit(void (*func)(void)) 
     184{ 
     185    if (atexit_count >= PJ_ARRAY_SIZE(atexit_func)) 
     186        return PJ_ETOOMANY; 
     187 
     188    atexit_func[atexit_count++] = func; 
     189    return PJ_SUCCESS; 
     190} 
     191 
     192 
     193/* 
     194 * pj_shutdown(void) 
     195 */ 
     196PJ_DEF(void) pj_shutdown() 
     197{ 
     198    int i; 
     199 
     200    /* Call atexit() functions */ 
     201    for (i=atexit_count-1; i>=0; --i) { 
     202        (*atexit_func[i])(); 
     203    } 
     204    atexit_count = 0; 
     205 
     206    /* Free exception ID */ 
     207    if (PJ_NO_MEMORY_EXCEPTION != -1) { 
     208        pj_exception_id_free(PJ_NO_MEMORY_EXCEPTION); 
     209        PJ_NO_MEMORY_EXCEPTION = -1; 
     210    } 
     211 
     212    /* Destroy PJLIB critical section */ 
     213    pj_mutex_destroy(&critical_section_mutex); 
     214 
     215    /* Free PJLIB TLS */ 
     216    if (thread_tls_id != -1) { 
     217        pj_thread_local_free(thread_tls_id); 
     218        thread_tls_id = -1; 
     219    } 
     220 
     221    /* Shutdown Winsock */ 
     222    WSACleanup(); 
     223} 
     224 
     225 
     226/* 
    180227 * pj_getpid(void) 
    181228 */ 
  • pjproject/trunk/pjlib/src/pj/pool_buf.c

    r768 r815  
    3030 
    3131static int is_initialized; 
    32 static long tls; 
     32static long tls = -1; 
    3333static void* stack_alloc(pj_pool_factory *factory, pj_size_t size); 
    3434 
    35 static pj_status_t initialize() 
     35static void pool_buf_cleanup(void) 
    3636{ 
     37    if (tls != -1) { 
     38        pj_thread_local_free(tls); 
     39        tls = -1; 
     40    } 
     41} 
     42 
     43static pj_status_t pool_buf_initialize() 
     44{ 
     45    pj_atexit(&pool_buf_cleanup); 
     46 
    3747    stack_based_factory.policy.block_alloc = &stack_alloc; 
    3848    return pj_thread_local_alloc(&tls); 
     
    6575 
    6676    if (!is_initialized) { 
    67         if (initialize() != PJ_SUCCESS) 
     77        if (pool_buf_initialize() != PJ_SUCCESS) 
    6878            return NULL; 
    6979        is_initialized = 1; 
  • pjproject/trunk/pjsip-apps/src/samples/aectest.c

    r648 r815  
    211211    pj_caching_pool_destroy( &cp ); 
    212212 
     213    /* Shutdown PJLIB */ 
     214    pj_shutdown(); 
    213215 
    214216    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/confsample.c

    r541 r815  
    461461    pj_caching_pool_destroy( &cp ); 
    462462 
     463    /* Shutdown PJLIB */ 
     464    pj_shutdown(); 
    463465 
    464466    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/level.c

    r541 r815  
    156156    pj_caching_pool_destroy( &cp ); 
    157157 
     158    /* Shutdown PJLIB */ 
     159    pj_shutdown(); 
     160 
    158161 
    159162    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/pjsip-perf.c

    r777 r815  
    895895        pj_caching_pool_destroy(&app.cp); 
    896896    } 
     897 
     898    /* Shutdown PJLIB */ 
     899    pj_shutdown(); 
    897900} 
    898901 
  • pjproject/trunk/pjsip-apps/src/samples/playfile.c

    r541 r815  
    196196    pj_caching_pool_destroy( &cp ); 
    197197 
     198    /* Shutdown PJLIB */ 
     199    pj_shutdown(); 
     200 
    198201 
    199202    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/playsine.c

    r635 r815  
    301301    pj_caching_pool_destroy( &cp ); 
    302302 
     303    /* Shutdown PJLIB */ 
     304    pj_shutdown(); 
     305 
    303306 
    304307    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/recfile.c

    r541 r815  
    191191    pj_caching_pool_destroy( &cp ); 
    192192 
     193    /* Shutdown PJLIB */ 
     194    pj_shutdown(); 
     195 
    193196 
    194197    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/resampleplay.c

    r541 r815  
    217217    pj_caching_pool_destroy( &cp ); 
    218218 
     219    /* Shutdown PJLIB */ 
     220    pj_shutdown(); 
     221 
    219222 
    220223    /* Done. */ 
  • pjproject/trunk/pjsip-apps/src/samples/siprtp.c

    r800 r815  
    380380        app.sip_endpt = NULL; 
    381381    } 
     382 
     383    /* Shutdown PJLIB */ 
     384    pj_shutdown(); 
     385 
    382386} 
    383387 
  • pjproject/trunk/pjsip-apps/src/samples/sndinfo.c

    r657 r815  
    286286    } 
    287287 
     288    /* Shutdown PJLIB */ 
     289    pj_shutdown(); 
     290 
    288291    return 0; 
    289292} 
  • pjproject/trunk/pjsip-apps/src/samples/streamutil.c

    r582 r815  
    517517    /* Destroy pool factory */ 
    518518    pj_caching_pool_destroy( &cp ); 
     519 
     520    /* Shutdown PJLIB */ 
     521    pj_shutdown(); 
    519522 
    520523 
  • pjproject/trunk/pjsip-apps/src/samples/tonegen.c

    r736 r815  
    147147    pj_caching_pool_destroy( &cp ); 
    148148 
     149    /* Shutdown PJLIB */ 
     150    pj_shutdown(); 
     151 
    149152 
    150153    /* Done. */ 
  • pjproject/trunk/pjsip/src/pjsip/sip_endpoint.c

    r810 r815  
    581581    /* Delete endpoint mutex. */ 
    582582    pj_mutex_destroy(endpt->mutex); 
     583 
     584    /* Delete module's mutex */ 
     585    pj_rwmutex_destroy(endpt->mod_mutex); 
    583586 
    584587    /* Finally destroy pool. */ 
  • pjproject/trunk/pjsip/src/pjsip/sip_transaction.c

    r756 r815  
    675675    pjsip_endpt_release_pool(mod_tsx_layer.endpt, mod_tsx_layer.pool); 
    676676 
     677    /* Free TLS */ 
     678    pj_thread_local_free(pjsip_tsx_lock_tls_id); 
     679 
    677680    /* Mark as unregistered. */ 
    678681    mod_tsx_layer.endpt = NULL; 
  • pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c

    r812 r815  
    708708    } 
    709709 
     710    /* Shutdown PJLIB */ 
     711    pj_shutdown(); 
     712 
    710713    /* Done. */ 
    711714    return PJ_SUCCESS; 
  • pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c

    r785 r815  
    475475    } 
    476476 
     477    /* Deinitialize sound subsystem */ 
     478    pjmedia_snd_deinit(); 
     479 
    477480    return PJ_SUCCESS; 
    478481} 
Note: See TracChangeset for help on using the changeset viewer.