質問

I have taken a look at an example i found on the internet of xerces generating XML. The Transcode function seems to perform some encoding but the buffer it populates to is never released. According to the documentation you must release ti manually:

NOTE: The returned buffer is dynamically allocated and is the responsibility of the caller to delete it when not longer needed. You can call XMLString::release to release this returned buffer.

could doc->release be releasing this from memory?

    XMLCh tempStr[100];

    XMLString::transcode("Range", tempStr, 99);
    DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr, 0);

    XMLString::transcode("root", tempStr, 99);
    DOMDocument*   doc = impl->createDocument(0, tempStr, 0);
    DOMElement*   root = doc->getDocumentElement();

    XMLString::transcode("FirstElement", tempStr, 99);
    DOMElement*   e1 = doc->createElement(tempStr);
    root->appendChild(e1);

    XMLString::transcode("SecondElement", tempStr, 99);
    DOMElement*   e2 = doc->createElement(tempStr);
    root->appendChild(e2);
    doc->release();
役に立ちましたか?

解決

Forgive me if I've misunderstood, but the version of transcode you're using there doesn't actutally create or return any buffers, because it is modifying the one you've supplied (tempStr) in place. Your example code will not leak memory.

The versions of transcode which do return a buffer (eg. every other version except the ones in your example code block) use a MemoryManager instance to perform the allocation. The documentation seems a little sparse here, but as far as I can tell the default XMLPlatformUtils::fgMemoryManager makes no claims to clean up any memory it has allocated. You should therefore assume that you need to destroy that returned buffer yourself.

他のヒント

No, doc->release() is not releasing the buffer.

As the documentation you quoted says, you would need to release the buffer with XMLString::release(), if you were calling a transcode function that returns a transcoded string:

char *str = XMLString::transcode(someStringVarToBeTranscoded);
XMLString::release(&str);

Your example does not need to do this, because you are using a transcode function that returns a boolean value.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top