C++ 如何解决报告驻留在STL字符串中的内存泄漏?

C++ 如何解决报告驻留在STL字符串中的内存泄漏?,c++,string,stl,valgrind,poco-libraries,C++,String,Stl,Valgrind,Poco Libraries,对于学校项目,我们必须通过网络发送大文件。我们必须使用Poco::XML作为数据 通过网络发送文件后,内存似乎无法释放 以下是接收部件上~9 Mb文件的示例: valgrind--leak check=full--show reachable=yes-v ourExecutable parms返回: 12,880,736 bytes in 37 blocks are definitely lost in loss record 101 of 101 at 0x4C2747E: oper

对于学校项目,我们必须通过网络发送大文件。我们必须使用Poco::XML作为数据

通过网络发送文件后,内存似乎无法释放

以下是接收部件上
~9 Mb
文件的示例:

valgrind--leak check=full--show reachable=yes-v ourExecutable parms
返回:

 12,880,736 bytes in 37 blocks are definitely lost in loss record 101 of 101
    at 0x4C2747E: operator new(unsigned long) (vg_replace_malloc.c:261)
    by 0x5A3AC88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.4.4/libstdc++.so.6.0.13)
    by 0x5A3BC4A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.4.4/libstdc++.so.6.0.13)
    by 0x5A3C1BB: std::string::reserve(unsigned long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.4.4/libstdc++.so.6.0.13)
    by 0x5A3C68E: std::string::append(std::string const&) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.4.4/libstdc++.so.6.0.13)
    by 0x5202359: Poco::XML::Element::innerText() const (in /home/tomwij/IGS/trunk/Project/external/lib/libPocoXML.so.8)
    by 0x4145BF: NodeProtocol::getChildNodeStrValue(Poco::XML::Element*, std::string) (NodeProtocol.cpp:82)
    by 0x41544F: NodeProtocol::deserialize(std::string const&) (NodeProtocol.cpp:200)
    by 0x40B088: Node::handleClientPacket(PriorityElement*) (Node.cpp:760)
    by 0x40A04C: Node::handlePackets() (Node.cpp:574)
    by 0x4078EA: Node::run() (Node.cpp:162)
    by 0x40772D: Node::activate() (Node.cpp:138)

 LEAK SUMMARY:
    definitely lost: 12,888,036 bytes in 190 blocks
    indirectly lost: 644,979 bytes in 1,355 blocks
      possibly lost: 10,089 bytes in 27 blocks
    still reachable: 306,020 bytes in 43 blocks
         suppressed: 0 bytes in 0 blocks
哪个叫

XMLString Element::innerText() const
{
    XMLString result;
    Node* pChild = firstChild();
    while (pChild)
    {
        result.append(pChild->innerText());
        pChild = pChild->nextSibling();
    }
    return result;
}
(注意,
XMLString
std::string

为什么STL字符串的
append
内存泄漏?

如果我只是赋值,而不是使用复制构造函数,则会出现同样的问题


编辑:

我正在Gentoox64(linux-2.6.34-Gentoo-r12)上使用最新的稳定GNU GCC 4.4.4

调用堆栈中的更多函数(剥离了大块代码/if结构):

其中,
FQUE
是:

priority_queue< PriorityElement*,  vector<PriorityElement*>, ComparisonFunction >
priority\u队列

我想对此发表评论,但显然我没有代表。您是否记得将
命令的析构函数设置为虚拟的?如果
name
data
PutCommand
的字段,而不是
Command
,并且
命令
析构函数不是虚拟的,当您在
handleClientPacket

中删除
命令时,可能无法正确释放它们。您没有在调用堆栈中显示
OurExecutable::handleClientPacket
函数的代码或其上的任何代码。您发布的代码中没有一个函数显示任何动态分配。所以漏洞不可能存在。你自己做过动态内存分配吗?使用了什么编译器(及其版本)?使用的是什么标准库实现?@MartinhoFernandes:您是否100%认为它不在Poco中?我已经添加了更多的信息。@Marlon:是的,除了反序列化中的AutoPtr之外;我添加了更多信息。很高兴你没有发表评论。显然,实际问题似乎离源头很远…:/经验教训:不必查看源代码,还要查看它存储在哪里以及发生了什么。
Command * NodeProtocol::deserialize(const string & msg)
{
    DOMParser xmlParser;

    // Get the root node.
    AutoPtr<Document> doc = xmlParser.parseString(msg);
    AutoPtr<Element> rootElement = doc->documentElement();

    string root = fromXMLString(rootElement->nodeName());
    string name = getChildNodeStrValue(rootElement, "name");
    string data = getChildNodeStrValue(rootElement, "data");
    return new PutCommand(name, data);
}
void Node::handleClientPacket(PriorityElement * prio)
{
        Command * command = NodeProtocol::deserialize(prio->fPacket);

        // CUT: Access some properties of command, let the command execute.

        delete command;
}
void Node::handlePackets()
{
    PriorityElement * prio = fQueue->top();
    fQueue->pop();

    if (prio->fSource == kCLIENT)
        handleClientPacket(prio);
    else if (prio->fSource == kNODE)
        handleNodePacket(prio);

    delete prio;
}
priority_queue< PriorityElement*,  vector<PriorityElement*>, ComparisonFunction >