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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:26:28+00:00 2026-05-29T06:26:28+00:00

will using a shared_ptr on class instance members clean up the new’ed objects correctly

  • 0

will using a shared_ptr on class instance members clean up the new’ed objects correctly when the owning “parent” drops out of scope? Like in the example below?

Code example below – so when the Family instance goes out of scope will it clean up/delete the people objects linked to in the map?

If so how can i confirm this? Are there tools i can use to show before and after – after showing no person object – and if i put the normal pointers back i should still see the person objects – thanks

class Family
{
private:
    //std::map<int ,Person*> _people;
    std::map<int ,std::tr1::shared_ptr<Person>> _people;

public:
    void AddPerson(int age)
    {
        //_people[age] = new Person(age);
        _people[age] = std::tr1::shared_ptr<Person>( new Person(age));
    }
};

    class Person
    {
    private:
        int _age;
    public: 
        Person(int age)
        {
            _age= age;
        }
        int GetName(){return _age;}
    };
  • 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-29T06:26:28+00:00Added an answer on May 29, 2026 at 6:26 am

    Go through it: Family goes out of scope, _people is destroyed, the map destructor removes all it’s elements, which means that for each element, the destructor is called. All shared_ptrS in the map decrement their reference count. If the reference count is now 0, the objects are destroyed. Although I don’t see why you would store a pointer (smart or not) in your example, I guess you have your reasons.

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

Sidebar

Related Questions

I have a problem using a shared_ptr of a base class, I can't seem
(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class
i am new to boost::shared_ptr using it the first time now. I have a
How to Write/Read a file to/from a network folder/share using python? The application will
Will using PinchMedia and including Core Location frameworks make it unusable on the iPod
Will using GetComInterfaceForObject and passing the returned IntPtr to unmanaged code keep the managed
I will be using jQuery's functionality to create portlet widgets for my web application
I am a student who will be using C++ next quarter. I really enjoyed
I have a php file which I will be using as exclusively as an
I am working on a project which will be using large datasets (both 2D

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.