Re: [Boost-users] boost shared_ptr and parent child relation ships

On Jun 20, 2008, at 8:38 AM, gast128 wrote:
When I use shared_ptr and weak_ptr in parent/child relationships, I tend to take a different tack. First of all, I like to use the concept of a "master" shared_ptr. That is, I attempt to design my architectures so that there is one shared_ptr which persists for the lifetime of the object being pointed to; other shared_ptrs are transient, in that they persist for a shorter period of time (typically which a particular piece of work is being performed). To pursue the metaphor, the "master" shared_ptr holds its object in existence. It is the pointer which (when reset) ultimately causes the object to be deleted. Using this concept, my parent object holds the master shared_ptr of its child. (Typically there are multiple children and their master shared_ptrs are stored in a vector.) The child holds a weak_ptr to its parent. In my work, typically both parent and child inherit from enable_shared_from_this. The "trick" is to treat the parent-child relationship seriously. Since child objects do not exist except in the context of a parent, it is logical to create a function member of the parent which is responsible for creating the child and establishing linkages. (Or, if this needs to occur elsewhere, the child is constructed by some other [third- party] object's member function which creates the child and links the child and parent together.) It should not be necessary to construct the child in the parent's constructor, nor should it be necessary to establish linkages with the parent in the child's constructor. The problems you are concerned about need not exist. As long as the construction of the child and its linkage to its parent occur in a single member-function, it should not be necessary for either the parent's or the child's constructor to be responsible for establishing the linkage. Cheers, Rick Aurbach, Ph.D. President and Chief Engineer Aurbach & Associates, Inc. 8233 Tulane Avenue, Suite B St. Louis, MO 63132 www: http://www.aurbach.com/ eMail: support@aurbach.com [business] rick@aurbach.com [personal] Fax: 314/678-0869 Phone: 800/774-7239 314/726-1321 "If it ain't broke and you haven't fixed it recently, it probably needs to be rewritten." - Richard Aurbach (1948- ) "Beware of bugs in the above code; I have only proved it correct, not tried it." - Donald Knuth, computer scientist (1938- ) "If in the last few years you haven't discarded a major opinion or acquired a new one, check your pulse. You may be dead." - Gelett Burgess (1866-1951) "A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing." - George Bernard Shaw (1856-1950) "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong." - Richard Feynman (1918-1988) "The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents." - Nathaniel Borenstein (1957-)

First of all, I like to use the concept of a "master" shared_ptr. That is, I
attempt to design my architectures so that there is one shared_ptr which persists for the lifetime of the object being pointed to; other shared_ptrs are transient, in that they persist for a shorter period of time (typically which a particular piece of work is being performed). To pursue the metaphor, the "master" shared_ptr holds its object in existence. It is the pointer which (when reset) ultimately causes the object to be deleted.
Using this concept, my parent object holds the master shared_ptr of its
child. (Typically there are multiple children and their master shared_ptrs are stored in a vector.) The child holds a weak_ptr to its parent. In my work, typically both parent and child inherit from enable_shared_from_this.
The "trick" is to treat the parent-child relationship seriously. Since child
objects do not exist except in the context of a parent, it is logical to create a function member of the parent which is responsible for creating the child and establishing linkages. (Or, if this needs to occur elsewhere, the child is constructed by some other [third-party] object's member function which creates the child and links the child and parent together.) It should not be necessary to construct the child in the parent's constructor, nor should it be necessary to establish linkages with the parent in the child's constructor. The problems you are concerned about need not exist. As long as the construction of the child and its linkage to its parent occur in a single member-function, it should not be necessary for either the parent's or the child's constructor to be responsible for establishing the linkage. what you ultimately describe is a 2 phase construction. If the invariant of your class is that the parent is only valid if it also has its required children, only after the second phase the invariant of the parent can be checked. Although 2 phase construction is regarded as a bad thing, I think it can not be circumvented in every situation (e.g. 2 phase construction allows polymorphic behavior and sometimes you need to make first A then B and then link). During construction and destruction (or in general during change) objects may be in an inconsistent state and dependent objects should be careful then.
participants (2)
-
gast128
-
Richard L. Aurbach