Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is shared_ptr returning an empty list?

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()":

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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_ptrs 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 newdelete.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading