7 Sep 03:44
newbie question on cyclic shared pointers
From: MGE <mick.elliot <at> gmail.com>
Subject: newbie question on cyclic shared pointers
Newsgroups: gmane.comp.lib.boost.user
Date: 2008-09-07 01:48:08 GMT
Subject: newbie question on cyclic shared pointers
Newsgroups: gmane.comp.lib.boost.user
Date: 2008-09-07 01:48:08 GMT
Hi guys, I have a data structure containing cycles of boost::shared_ptrs. I understand that this is not advisable since the cycles may not be deleted when shared pointers go out of scope. Possible solutions include the use of weak_ptrs or, alternatively, manually removing the cycles before deletion of pointers. I am finding it difficult to use weak_ptrs because I cannot guarantee that the single shared_ptr will outlive the numerous weak_ptrs. So I am trying to manually break the cycle at deletion. I would be grateful if somebody could take a look at the code below, which illustrates my basic approach. Assuming that I stringently break all cycles when a Child class or Parent class goes out of scope, would this approach avoid memory leaks? Would it be possible to put code into the destructor of Parent and Child classes that automatically sets the shared pointer owned by the class to null (not using the reset() function but by using the method below) hence guaranteeing that cycles are broken at deletion? Thanks for any advice! Best regards, MGE --------------------- struct Child;(Continue reading)
RSS Feed