GSoap服务器内存泄漏问题

在使用GSoap2.7.10构建的服务器程序中,发现存在内存泄漏问题,尤其是在压力测试下内存持续增长。然而,通过在循环中调用`soap_destroy()`和`soap_end()`或者使用`svr.run()`,可以成功解决这个问题。官方文档提到在DEBUG模式下,内存释放需调用`soap_done()`,但在实际测试中,此解决方案在Debug和Release版本都有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

version 1.0
2008-11-25
    写了个GSoap2.7.10的服务器小程序,代码很简单:

    RecSoapBindingService svr;
    int m = svr.bind(0, 80, 100);
    if(m<0)
    {
        soap_print_fault(&svr, stderr);
        return 0;
    }
   
    fprintf(stderr, "Socket connection successful: master socket = %d/n", m);
   
    for(;;)
    {
        m = svr.accept();
        if (m < 0)
        {
            soap_print_fault(&svr, stderr);
            exit(-1);
        }
        fprintf(stderr, "Socket connection successful: slave socket = %d/n", m);
        svr.serve();
        //break;
    }

    测试发现有内存泄漏问题:压力测试下看见内存一直在涨,但正常退出(通过在循环中加break使程序正常结束)时内存能够释放完全(VC环境中没有检测到内存泄漏);Debug与Release版本都是这样。
    GSoap官方网站上(http://www.cs.fsu.edu/~engelen/soap.html)有一段说明,不知道针对哪个版本:

The gSOAP engine uses a memory management method to allocate and deallocate memory. The deallocation is performed with soap_destroy() followed by soap_end(). However, when you compile with -DDEBUG or -DSOAP_MEM_DEBUG then no memory is released until soap_done() is invoked. This ensures that the gSOAP engine can track all malloced data to verify leaks and double frees in debug mode. Use -DSOAP_DEBUG to use the normal debugging facilities without memory debugging. Note that some compilers have DEBUG enabled in the debug configuration, so this behavior should be expected unless you compile in release config.

        于是在
svr.serve();后加入代码:
        soap_destroy(&svr);
        soap_end(&svr);

        问题解决——与说明不符的是,没发现Debug版与Release版的区别。

        也可以直接调用
svr.run()避免这一堆处理。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值