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

  • Home
  • SEARCH
  • 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 7928209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:40:38+00:00 2026-06-03T19:40:38+00:00

I have a function that returns a shared pointer to an object (it is

  • 0

I have a function that returns a shared pointer to an object (it is difficult to include the MyObject definition because of many large dependencies):

std::shared_ptr<MyObject> f(std::string params)
{
  return std::shared_ptr<MyObject>(new MyObject(params));
}

Does anyone know why this code works:

Case 1: no errors with valgrind -v --tool=memcheck

std::shared_ptr<MyObject> obj_ptr = f("hello");
MyObject obj = *obj_ptr;

While this code crashes:

Case 2: crashes and gives several errors with valgrind -v --tool=memcheck

MyObject obj = *f("hello");

The MyObject class has a working assignment operator and copy constructor (both verified in Case 1).

I have also tried creating a std::shared_ptr<MyObject> (via f), copying that to a pointer, copying the pointer to an object on the stack, and deleting the pointer. The final object on the stack is still fine:

Case 3: no errors with valgrind -v --tool=memcheck

std::shared_ptr<MyObject> obj_ptr = f("hello");
MyObject * obj_ptr2 = new MyObject(*obj_ptr);
MyObject obj3 = *obj_ptr2;
delete obj_ptr2;
obj3.print();

Is the error perhaps because std::shared_ptr is created as an rvalue, and frees its memory as soon as the * operator runs?

  • 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-03T19:40:39+00:00Added an answer on June 3, 2026 at 7:40 pm

    The problem is (almost certainly) that you’re shallow-copying one of the members of MyObject in its copy constructor. Then you either try to access the shallow data that’s no longer valid, or you double delete it.

    Consider the cases:
    In the first and third cases, the very first object from which all copies are made is still alive when you act on the stack object. In the second case the shared_ptr goes away after the copy construction, invoking the destructor of MyObject.

    If you changed the third case thusly, I suspect it would crash:

    MyObject * obj_ptr2 = new MyObject("hello");
    MyObject obj3 = *obj_ptr2;
    delete obj_ptr2;
    obj3.print();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function that returns a DataTable : Public Shared Function GetDataTable(ByVal PageSize
I have a function that returns an entry on a dictionary, based on the
I have a function that returns a date from a stored procedure, and it
I have a function that returns a table. The returned table contains (among other
I have a function that returns json (networks_function.php builds up some arrays and encodes
I have a function that returns the highest value of an attribute in an
Suppose I have a function that returns a closure: sub generator { my $resource
How can we new primitive types in JNI. I have a function that returns
I have a simple function that returns an NSString after decoding it. I use
We have a table value function that returns a list of people you may

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.