首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache线程的apr_pool_destroy()安全吗?

问Apache线程的apr_pool_destroy()安全吗?
EN

Stack Overflow用户
提问于 2010-09-02 13:07:54
回答 1查看 1.1K关注 0票数 1

我的应用程序是用apache构建的,在windows上运行。我使用createThread()创建了一个线程,然后对每个线程执行以下命令:

代码语言:javascript
复制
ap_run_sub_req( subrequest );   
ap_rflush( subrequest );  
ap_destroy_sub_req( subrequest );  

ap_destroy_sub_request反过来调用apr_pool_destroy()函数。

ap_run_sub_req()为池分配内存,ap_destroy_sub_req()释放分配的内存。

如果在线程内调用apr_pool_destroy(),则分配的内存不会被释放,因为我的应用程序存在内存泄漏。我在任何apache文档中都找不到任何提到apr_pool_destroy()是非线程安全函数的地方。

如何解决这个问题?如何释放线程内已分配的池?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2010-09-11 01:20:22

以下是apr_pool_destroy()的源代码

代码语言:javascript
复制
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上发帖。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3623972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档