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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:43:14+00:00 2026-06-18T02:43:14+00:00

I have a directed acyclic graph implemented by Graph and Node classes. Each node

  • 0

I have a directed acyclic graph implemented by Graph and Node classes. Each node has a list of pointers to childern and a list of pointers to parents. I added parents recently because some algorithms demanded fast access to parent list and the graph is small, just few connections per node, so there’s no memory issue.

The Child list uses std::shared_ptr so that nodes are kept in memory at least as long as they have parents. But I don’t want a node to own its parents, so I used weak_ptr for the pointers to parents.

But then there was a problem with the algorithms. An algorithm must create a new shared_ptr from the weak_ptr, so I can’t directly use operator==, and using standard functions such as std::find() require writing a lambda function which called my_weak_ptr.lock() and then compares it to some shared_ptr.

If I switch to shared_ptr, any small bug in the code reponsible for node removal may lead to a memory leak. Or if I have a pointer to a node already removed, the code will be able to access a node not supposed to exist, so finding some bugs may become a lot harder. But working with shared_ptr is as safe as weak_ptr in terms of not dereferencing/deleting/etc. when not supposed to, (so it’s better than raw C++ pointer) and std::find() can be used directly because shared_ptr can be dereferenced, unlike weak_ptr.

Is there a “better” design here, or it’s an issue of this specific situation depending on e.g. how much does it matter if I do the extra operation of weak_ptr::lock() or take a risk of hard-to-find bugs?

  • 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-18T02:43:15+00:00Added an answer on June 18, 2026 at 2:43 am

    As you said yourself, using shared_ptr in both directions will create circles that create mem leaks and are hard to find and to break – you will lose (nearly) all of the benefits shared_ptr provides. So weak_ptr it shall be.

    You say your algorithms have to lock the weak_ptr – I beg to differ. The algorithms have to get a parent shared_ptr from the node. It’s the node’s task to lock the parent weak_ptr and give back the result, either set correctly to a parent node or to NULL.

    It’s an implementation detail wether the nodes store their parents as shared_ptr or weak_ptr. Encapsulate that detail by only providing shared_ptrs to any clients.

    class Node
    {
      /* ... */
      std::weak_ptr<Node> parent;
    public:
      std::shared_ptr<Node> getParent()
      {
        return parent.lock();
      }
    };
    

    Edit:
    Of course conceptually the same applies if there’s more than one parent.

    Edit2:
    In the comments you mention algorithms iterating over your parents list, making it necessary to write lambdas for each algorithm. If you use those algorithms often, consider to write an iterator adapter that automatically locks the target weak_ptr and gives back a shared_ptr:

    template <class WPIterator>
    struct LockTheWeakIterator
    {
      //static_assert that WPiterator's value_type is some weak_ptr
      //typedef all those iterator typedefs
      typedef typename WPIterator::value_type::element_type element_type;
    
      shared_ptr<element_type> operator*()
      { return iter->lock(); }
    
      //provide all the other operators - boost.operators might help with that...
    
      WPIterator iter;
    };
    
    template <class IT>
    LockTheWeakIterator<It> lockTheWeak(It iter);
    
    
    //somewhere...
    auto theParentIter = std::find_if(lockTheWeak(parents.begin()), 
      lockTheWeak(parents.end()), 
      whatIAmLookingFor);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directed acyclic graph, composed of Node objects. Each node has a
I have a directed acyclic graph (DAG) with a weight associated with each node.
I have a directed-graph, each node is a complex data type. Does anyone know
I have an acyclic directed graph. I would like to assign levels to each
This is a supervised learning problem. I have a directed acyclic graph (DAG). Each
I have a d3 force-directed graph that contains a group of nodes: var node
I have a directed, positive weighted graph. Each edge have a cost of use.
I have an adjacency matrix of a directed acyclic graph represented by a 2D
I have a directed acyclic graph with nodes that are lists of entries that
I have a large (100,000+ nodes) Directed Acyclic Graph (DAG) and would like to

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.