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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:59:43+00:00 2026-05-25T11:59:43+00:00

I have a set of objects in a vector of pointers to their baseclass

  • 0

I have a set of objects in a vector of pointers to their baseclass held inside a manager:

std::vector<object*> objectVec;

Classes may wish to spawn one of these objects using the Add() method in the manager. The problem is that they then subsequently need to set or update these objects themselves. I’ve decided to have Add() return a pointer to the object itself, which is stored in whatever class has decided to spawn one. The problem is dealing with the case where the object behind that pointer may have been deleted.

Add looks like this:

object* ObjectManager::Add(object* obj)
{
   objectVec.push_back(obj);
   return objectVec.back();
}

and used like this:

objectptr = ObjectManager::OMan()->Add(new object());

Where objectptr is a member of whatever class has called the function. So should that particular object be deleted, the pointer returned by Add would point to rubbish.

Is it my responsibility to ensure that whateverclass::objectptr is always set to NULL if this object is deleted? Or can this be dealt with using some sort of smart pointer? The problem being that I don’t need to use a smart pointer to deal with the possibility of a memory leak, but to deal with the case where the stored pointer has become invalid.

Please let me know if i’ve been unclear, or if the question is badly formed.

Any help would be greatly appreciated.

  • 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-25T11:59:44+00:00Added an answer on May 25, 2026 at 11:59 am

    You likely want weak_ptr and shared_ptr. shared_ptr is a general smart pointer class. weak_ptr is an observer of shared_ptr. When all the references of the shared_ptr go away, instances of weak_ptr “become null” and are easier to deal with than a pointer to a deleted object.

    These classes come with Boost.
    http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm

    http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/weak_ptr.htm

    And if I’m not mistaken, there are equivalents built into std namespace on compilers that implement newer C++0x standards. Visual C++ keeps has this built in.

    http://blogs.msdn.com/b/vcblog/archive/2011/02/16/10128357.aspx

    Oh shoot, looks like everyone else beat me to the answer…

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

Sidebar

Related Questions

I have a set of objects in a Vector from which I'd like to
I have a set of objects which I iterate through, however I may decide
I have an object that is mapped to have a set of objects, very
I have a set of tree objects with a depth somewhere in the 20s.
As described in another question , I have a set of Model objects and
I have a asp:menu object which I set up to use a SiteMapDataSource but
I have a custom security principal object which I set in the global.asax for
I have been getting an error in VB .Net object reference not set to
I'm sure we all have received the wonderfully vague Object reference not set to
We have set up a system where notifications get sent to a user with

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.