Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:22:12+00:00 2026-06-09T14:22:12+00:00

I need parse some XML and wrote some helpers. I am not expert in

  • 0

I need parse some XML and wrote some helpers. I am not expert in C++, actually I wrote with c more then seven years ago. So, I would to make sure, is the approach, what i use correct or not 🙂

1) I implemented some simple helpers, to take care about exceptions.
For example:

CComPtr<IXMLDOMElement> create_element(CComPtr<IXMLDOMDocument> xml_doc, string element_name) {
    CComPtr<IXMLDOMElement> element;

    HRESULT hr = xml_doc->createElement((BSTR)element_name.c_str(), &element);

    if (FAILED(hr))
        hr_raise("Failed to create XML element '" + element_name + "'", hr);

    return element;
}

and use it like this:

void SomeClass::SomeMethod() {
    CComPtr<IXMLDOMElement> element = xmlh::create_element(xml_doc, "test");
    //..
    // save xml to file
}

is it ok? I mean can i return smart pointer as function result? Is this approach free from leaks?

2) Also i use some smartpointer as Class Members.
Like this:

class XMLCommand {
    public:
        XMLCommand(std::string str_xml);
        ~XMLCommand(void);
    protected:
        CComPtr<IXMLDOMDocument> xml_doc;
}


XMLCommand::XMLCommand(string str_xml) {
    xml_doc = xmlh::create_xml_doc();
}

// some methods below uses xml_doc

The question is the same, is it correct and free from leaks?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:22:14+00:00Added an answer on June 9, 2026 at 2:22 pm

    That will work fine. When returning a smart pointer from function, the result is stored before the temporaries are destructed, so as long as you store it in a CComPtr<IXMLDOMElement> when you call create_element you will get the desired results (e.g., CComPtr<IXMLDOMElement> resElem = create_element(...);. Optimized C++ will very likely even not bother with temporaries and such and just use resElem instead of element inside your create_element() method, speeding up the process (google Return Value Optimization for details).

    The latter case is pretty much textbook smart-pointer usage. I can’t think of a case that will fail. One danger when using smart pointers in general though is to be aware of and/or avoid circular dependencies, which can cause smart pointers to never delete their contained object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse a XML String so I can use some of the
I need to parse some XML that triggers an NSURLConnection. After the parsing finished,
I need to parse a XML file from the website.I have went through some
I wrote a RSS based application that will fetch some XML files, parse them
I'm modifying some legacy code and need to parse/write to XML with C#. I'm
I need to download and parse some XML data, and to store it in
I have a problem right now, I need to parse an XML but some
I need to parse some text in a UITextField and turn it into a
I need parse through a file and do some processing into it. The file
Using Ruby, I'm trying to parse some documentation in which I need to split

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.