473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

cycles of shared_ptr avoid using weak_ptr.

below lines are from boost"Because shared pointer implementation uses
reference counting, cycles of shared_ptr instances will not be
reclaimed. For example, if main() holds a shared_ptr to A, which
directly or indirectly holds a shared_ptr back to A, A's use count
will be 2. Destruction of the original shared_ptr will leave A
dangling with a use count of 1. Use weak_ptr to "break cycles.""
I was trying to create a cycle of smart pointer with the below code
class a{
public:
inline a(){std::cout<<"constructor called";}
shared_ptr<aclass_memeber;

};

void main(){
shared_ptr<amainvar(new a);
std::cout<<mainvar.use_count()<<std::endl;
mainvar->class_memeber.reset(mainvar.get());
std::cout<<mainvar->class_memeber;
int i;
std::cin>>i;

}

there is some mistake, can you please explain how the cyclic smart
pointer is created and how to avoid the same with weal_ptr
Jul 28 '08 #1
0 1263

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raf256 | last post by:
Hi, how to convert following C++ code: foobar *ptr = new foobar(5); //... if (something()) ptr = NULL; if (!ptr) return no_object_is_selected_apparently; // ... if (!ptr) throw...
1
by: Goran | last post by:
Hi all! I am trying to use boost smart pointers for some multithreaded work in the following way: 1. I use shared_ptr to wrap stuff in thread 1 2. I create weak_ptr-s in thread 1 and pass...
3
by: Emmanuel Deloget | last post by:
Hello y'all, I'm currently writing a serie of blog tickets about the TR1, both from a definition point of view and from an implementation point of view. The next iteration in the series deals...
1
by: puzzlecracker | last post by:
When would you use which?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.