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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:26:42+00:00 2026-06-15T19:26:42+00:00

I have been messing around with using smart pointers in my latest C++ endeavor

  • 0

I have been messing around with using smart pointers in my latest C++ endeavor for memory management. Part of this is that I am using a series of maps to tie a string to a class (called an IBlockInput) and the class to its current value inside a “context”. I see two options, but both have the same problem:

Option 1: The class’s current value is stored inside the class, sorted by context

Option 2: The class’s current value is stored inside the context, sorted by class

The problem here is that either object may die before the other. A Context may be destroyed while all the inputs remain or an input may be destroyed while the context remains. When one dies, the resources need to be released for it. What I have so far is (using option 2):

std::map<std::string, boost::weak_ptr<IBlockInput> > inputs;
std::map<boost::weak_ptr<IBlockInput>, boost::shared_ptr<std::vector<double> > > inputValues;

The reason they need to be separate like this is that other classes (IOutputBlocks) need to access and set the values for the IInputBlocks based on their pointer.

There are a few questions I have about this:

Question 1: Can I access the inputValues map like a normal map and just pass in the pointer (not the boost::shared_ptr/boost::weak_ptr…the T*) to access it? I can use weak_ptrs or shared_ptrs as well if that is the actual way to do it

Question 2: I am using weak pointers to let the objects die if needed (If I was doing option 1 above, I would have to weak pointers pointing to the context). What happens if the weak pointer expires and I try to access it in the map? Is the data it points to lost to the ages or does it still work with the raw pointer?

Question 3: Perhaps a better way to do this is to use the raw pointers as the keys/values and then have a supplementary map with the weak pointers (for a live check of sorts) like so:

std::map<std::string, IBlockInput*> inputs;
std::map<IBlockInput*, boost::shared_ptr<std::vector<double> > inputValues;
std::map<IBlockInput*, boost::weak_ptr<IBlockInput> > pointerCache;

Then I could do a “live check” using the pointerCache to see if I should keep the vector of doubles alive and manage the memory that way. NOTE: The inputs vector is refreshed relatively often.

What is the recommended practice for this? I am very new to boost and using smart pointers in general (I’ve been pampered by C# at work for the past several months and not had to worry about this kind of thing).

  • 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-15T19:26:43+00:00Added an answer on June 15, 2026 at 7:26 pm

    std::map<boost::weak_ptr<IBlockInput>, boost::shared_ptr<std::vector<double> > > inputValues; — this is bad. When a weak_ptr dies, it will compare equal to nullptr, and its order is not maintained. You’ll get undefined behavior (and in practice, infinite loops and crashes)

    Be careful what you use for keys in std::maps.

    Using raw pointers that may be deallocated is no good due to the ABA problem, where a valid pointer becomes invalid, then a new pointer is allocated equal to it. (there is exploiting implementation details of make_shared to make this kind of thing work in practice, but that is messy and dangerous)

    You probably need some kind of double-indirection, where you have the equivalent of pointers to pointers. I’ll call them handles. The inner pointer of the handle expiry causes it to unregister the handle in maps, and when it is out of every map it destroys itself. I don’t know if it will work, but it might. I suspect it would need some fiddling first.

    Handles would be ordered by their (outer) pointer value, so would maintain ordering. To determine if a handle is valid, you have to both check the outer and inner pointer’s validity (so non-null handles can be invalid to “dereference”).

    But I cannot think of a way to make this clean.

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

Sidebar

Related Questions

I have been messing around with OpenGL lately, and I noticed that the windows
I have been messing around with recursion today. Often a programming technique that is
I have been messing around with HtmlUnit for a little bit and particularly this
I've been messing around with this for some time. I recently started using the
I've been messing around with the Python/C API and have this code: #include <Python.h>
I have been messing around with writing some stored procedures in .NET code with
I have been messing around with Leaks trying to find which function is not
I have been messing around with my meta descriptions, trying to make them more
I have been messing around with iTunes COM from python. However, I haven't been
Hey all, I've been messing around with this awesome library on my *nix box,

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.