2010-09-02 90 views
1

我的應用程序是用apache構建的,可以在windows上運行。我創建使用的CreateThread()一個線程,然後爲每個線程執行如下:apache線程的apr_pool_destroy()是否安全?

ap_run_sub_req(subrequest); 
ap_rflush(subrequest); 
ap_destroy_sub_req(subrequest); 

反過來ap_destroy_sub_request調用apr_pool_destroy()函數。

爲池和ap_destroy_sub_req()分配的內存ap_run_sub_req()釋放分配的內存。

如果apr_pool_destroy()在一個線程中被調用,那麼分配的內存不會被釋放,因爲我的應用程序有內存泄漏。我在任何apache文檔中找不到任何提及apr_pool_destroy()是非線程安全函數。

這個問題怎麼解決?我怎樣才能釋放線程內分配的池?
感謝

回答

1

這裏的源代碼apr_pool_destroy()

APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool) 
{ 
    apr_memnode_t *active; 
    apr_allocator_t *allocator; 

    /* Run pre destroy cleanups */ 
    run_cleanups(&pool->pre_cleanups); 
    pool->pre_cleanups = NULL; 

    /* Destroy the subpools. The subpools will detach themselve from 
    * this pool thus this loop is safe and easy. 
    */ 
    while (pool->child) 
     apr_pool_destroy(pool->child); 

    /* Run cleanups */ 
    run_cleanups(&pool->cleanups); 

    /* Free subprocesses */ 
    free_proc_chain(pool->subprocesses); 

    /* Remove the pool from the parents child list */ 
    if (pool->parent) { 
#if APR_HAS_THREADS 
     apr_thread_mutex_t *mutex; 

     if ((mutex = apr_allocator_mutex_get(pool->parent->allocator)) != NULL) 
      apr_thread_mutex_lock(mutex); 
#endif /* APR_HAS_THREADS */ 

     if ((*pool->ref = pool->sibling) != NULL) 
      pool->sibling->ref = pool->ref; 

#if APR_HAS_THREADS 
     if (mutex) 
      apr_thread_mutex_unlock(mutex); 
#endif /* APR_HAS_THREADS */ 
    } 

    /* Find the block attached to the pool structure. Save a copy of the 
    * allocator pointer, because the pool struct soon will be no more. 
    */ 
    allocator = pool->allocator; 
    active = pool->self; 
    *active->ref = NULL; 

#if APR_HAS_THREADS 
    if (apr_allocator_owner_get(allocator) == pool) { 
     /* Make sure to remove the lock, since it is highly likely to 
     * be invalid now. 
     */ 
     apr_allocator_mutex_set(allocator, NULL); 
    } 
#endif /* APR_HAS_THREADS */ 

    /* Free all the nodes in the pool (including the node holding the 
    * pool struct), by giving them back to the allocator. 
    */ 
    allocator_free(allocator, active); 

    /* If this pool happens to be the owner of the allocator, free 
    * everything in the allocator (that includes the pool struct 
    * and the allocator). Don't worry about destroying the optional mutex 
    * in the allocator, it will have been destroyed by the cleanup function. 
    */ 
    if (apr_allocator_owner_get(allocator) == pool) { 
     apr_allocator_destroy(allocator); 
    } 
} 

從外觀上來看,它不是線程安全的,但我不是一個C專家。你應該在the APR mailing list上發帖。