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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:41:06+00:00 2026-06-11T01:41:06+00:00

I was trying with the cyclic references for boost::shared_ptr , and devised following sample:

  • 0

I was trying with the cyclic references for boost::shared_ptr, and devised following sample:

class A{ // Trivial class
public:
    i32 i;
    A(){}
    A(i32 a):i(a){}
    ~A(){
        cout<<"~A : "<<i<<endl;
    }
};

shared_ptr<A> changeI(shared_ptr<A> s){
    s->i++;
    cout<<s.use_count()<<'\n';

    return s;
}

int main() {

    shared_ptr<A> p1 = make_shared<A>(3);
    shared_ptr<A> p2 = p1;
    shared_ptr<A> p3 = p2;
    shared_ptr<A> p4 = p3;

    p1 = p4; // 1) 1st cyclic ref.
    cout<<p1.use_count()<<'\n';

    p1 = changeI(p4); // 2) 2nd cyclic ref.

    cout<<p1.use_count()<<'\n';

//  putchar('\n');
    cout<<endl;
}

which outputs

4
5
4

~A : 4

Is it that I’ve misinterpreted the cyclic references mentioned for boost::shared_ptr? Because, I expected different output thinking of indirect references to p1 after comments 1) and 2).
So this code doesn’t require boost::weak_ptr! So what are the cyclic references where weak_ptrs would be required?

Thanks in advance.

  • 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-11T01:41:07+00:00Added an answer on June 11, 2026 at 1:41 am

    Yes, you have misinterpreted this. In your example, all the pointers are pointing to the same object, not forming any cycles.

    The assignment of p4 to p2 is a no-op, since those pointers were already equal to begin with.

    Here’s an example with real cyclic references, maybe that will clear things up:

    struct A
    {
      std::shared_ptr<A> ptr;
    };
    
    void main()
    {
      std::shared_ptr<A> x=std::make_shared<A>();
      std::shared_ptr<A> y=std::make_shared<A>();
    
      x->ptr = y; // not quite a cycle yet
      y->ptr = x; // now we got a cycle x keeps y alive and y keeps x alive
    }
    

    You can even make this even simpler:

    void main()
    {
      std::shared_ptr<A> x=std::make_shared<A>();
    
      x->ptr = x; // never die! x keeps itself alive
    }
    

    In both examples, the objects in the shared_ptrs are never destructed, even after you leave main.

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

Sidebar

Related Questions

I have the following cyclic dependency problem I am trying to solve: typedef std::map<int,
Is there any tips/tricks for finding cyclic references of shared_ptr's? This is an exmaple
I am trying to use the CycleRecoverable interface to manage cyclic issues in my
I am trying to run my code so it prints cyclic permutations, though I
I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm
The following pseudo-Scala yields an illegal cyclic reference error: trait GenT[A] trait T extends
I'm desperately trying to solve the following: trait Access[Res[_]] { def access[C]: Res[C] }
I'm trying to write the following Perl subroutine. Given are an array a of
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the

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.