I am working with Xerces-c (version 3.2) and I have this function that wraps around the XMLString::transcode method.
XMLCh* class_name::transcode(const std::string text) {
auto returned = std::shared_ptr<XMLCh>(XMLString::transcode(text.c_str()), class_name::releaseXMLCh);
return returned.get();
}
(XMLCh is a char16_t)
The point of this is to have a shared pointer that will call "releaseXMLCh()" when it is no longer needed. Here is the implementation of "releaseXMLCh()":
void class_name::releaseXMLCh(XMLCh* text) {
XMLString::release(&text);
}
According to the Xerces-c documentation, XMLString::release needs to be called after calling XMLString::transcode to avoid a memory leak.
However, this function only ever returns "", regardless of what the input string is. I suspect that I am doing something wrong with the shared_ptr, as calling XML::transcode on its own works perfectly fine.
Does anyone know why this is happening? Thanks!
>Solution :
Think about it:
- you create a local
std::shared_ptr
. - You get the (raw) pointer to the internal object.
- The local
std::shared_ptr
goes out of scope and is destructed.
How many std::shared_ptr
s will there be pointing to the internal object?
None. So it’s also destructed. Thus, this will not work this way.
You are returning a pointer to a destroyed object, which causes Undefined Behavior.
You need a persisting smart pointer object (could likely even be a unique_ptr
) or just use the old-fashioned new
–delete
.