【问题标题】:gSOAP C++ client memory leakgSOAP C++ 客户端内存泄漏
【发布时间】:2010-08-10 15:10:02
【问题描述】:

我已阅读 gSOAP 文档并看到有人提到应该调用soap_destroy(soap) 和soap_end(soap) 等,但是它们始终是对服务对象进行一次调用的示例。我使用的服务每次调用都会返回大约 40KB 的文本。我的问题是每个请求的内存使用量线性增长大约相同的大小。我在 getWords 中添加了 soap_destroy(service->soap) 无济于事。谁能指出此代码 sn-p 中缺少哪些清理代码?请求程序应该连续运行几天,所以我担心的是每次请求清理而不是关闭。

我在下面发布了一个基于http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=43 的类似示例(没有错误检查),(它返回文本块对吗?)。非常感谢任何帮助!

#include "soapBibleWebserviceSoapProxy.h"
#include "BibleWebserviceSoap.nsmap"
#include <iostream>
extern "C" {
#include <unistd.h>
}

struct Service
{
    BibleWebserviceSoap service;

    std::string getWords(std::string &title, int chapter)
    {   
        _ns1__GetBibleWordsByBookTitleAndChapter req;
        _ns1__GetBibleWordsByBookTitleAndChapterResponse resp;
        req.BookTitle = &title;
        req.chapter   = 1;

        service.__ns2__GetBibleWordsByBookTitleAndChapter(&req, &resp);

        return *(resp.GetBibleWordsByBookTitleAndChapterResult);
    }
};

int main(int argc, char* argv[])
{
    Service s;
    std::string genesis("Genesis");
    for (int i=0; i<360; ++i)
    {   
        sleep(2);
        std::cout << s.getWords(genesis,1) << std::endl;
    }
    return 0;
}

【问题讨论】:

    标签: c++ linux memory-leaks client gsoap


    【解决方案1】:

    在 Valgrind(valgrind.org - 通常默认安装在 Linux 上)下运行您的应用程序 - 这是追踪内存泄漏的最简单方法。

    拨打 1,000 多次电话后,您会在关机时看到泄漏。 如果在关闭时未显示泄漏,则某些列表或地图会收集条目但仅在关闭时释放它们 - 在这种情况下使用 Massif(Valgrind 的一部分) - 它也是一个很棒的工具。

    这不是一个直接的答案,但是占用内存的分配的堆栈跟踪通常有助于查明泄漏的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      相关资源
      最近更新 更多