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 7012091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:12:39+00:00 2026-05-27T22:12:39+00:00

I’m using a unique_ptr to pass a const wchar_t pointer to a function. In

  • 0

I’m using a unique_ptr to pass a const wchar_t pointer to a function. In the following I would like to give a short example:

bool MyClass::foo(unique_ptr<const wchar_t> display_name) {
  bool result = false;
  ....
  // Do something
  result = DoWork(display_name.get());
  ....
  // I have to call release to prevent getting an error
  display_name.release();
  return result;
}

Until now I thought I don’t have to call the release() method of a unique_ptr before leaving the scope (return from function) because the content of the unique_ptr will automatically be deleted if the unique_ptr runs out of scope. But if don’t call the release() method I get the following error (VS 2010):

enter image description here

I think this error message occurs because the memory isn’t correctly freed? What’s the recommended approach dealing with unique_ptr, which is passed as an argument?

  • 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-05-27T22:12:39+00:00Added an answer on May 27, 2026 at 10:12 pm

    Based on your description and comment I believe this is more what you are looking for:

    bool MyClass::foo(const std::wstring& display_name)
    {
      bool result = false;
    
      // Do something
      result = DoWork(display_name.c_str());
    
      return result;
    }
    

    This string object is a STL container of wchar_t that has various helpful methods. One of which is c_str() which returns a c-style const wchar_t* version of the string contents for backwards compatibility with older code. Your DoWork function may be such a function that requires a c-style string so the above will work for you.


    Now onto smart pointers 101:

    In C++11 std::unique_ptr can be used to automatically destruct objects allocated on the heap freeing from the worries of exception unsafe code and manual memory management which can lead to memory leaks and other bad things. Say you had the following code:

    void LeakyFunction()
    {
        double* ptr = new double;
    }
    

    Obviously that’s a memory leak as once the scope of LeakyFunction ends we’ve lost the pointer on the stack and no longer can delete what it was pointing to on the heap. So we write this:

    void LessLikelyLeakyFunction()
    {
        double* ptr = new double;
        // do stuff
        delete ptr; // clean up heap
    }
    

    That’s well and dandy unless you have an early return or throw an exception during the do stuff portion of code which puts you back to the same problem as before. So people started writing custom smart pointer classes which owned raw memory allocation by allocating on construction and deallocating on destruction. This concept has now become standard via things like std::unique_ptr so we can do the following:

    void SmartFunction()
    {
        std::unique_ptr<double> ptr(new double);
        // do stuff
    } // unique_ptr is smart and calls delete for you
    

    In this way no matter when the scope of the function ends you don’t have to worry things will be cleaned up properly. In short if you aren’t using new you don’t need a smart pointer and if you are then you probably should use a smart pointer to make your code more robust.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.