Article View: alt.comp.lang.learn.c-c++
Article #138Re: Destructing encapsulated objects
From: Phlip
Date: Fri, 26 Mar 1999 00:00
Date: Fri, 26 Mar 1999 00:00
55 lines
3013 bytes
3013 bytes
Michael <&&schwager@u.arizona.edu> wrote:
>I am running into some problems that are pretty general, but I'm
not sure
>how to solve them. Basically they are like this: A parent window
(or other
>generic object) has pointers-to several child windows (or other
object).
>When a child window is deconstructed (pathway 1), it has to call a
function
>foo() in the parent. However, when the parent is deconstructed
first, and
>subsequenty calls the child's deconstructor (pathway 2), I don't
want the
>child's deconstructor calling the parent's foo() function, because
this is
>illegal (but it will compile) and definitely causes a crash. I
tried moving
>the parent's function to ITS parent, but the same problem remains,
just one
>level higher. I'm sure someone else has solved this problem. Any
ideas?
>I'd like the child to know when NOT to call the parent function,
i.e., the
>child should know when the PARENT is in ITS deconsructor, or better
yet,
>some general solution to this problem.
Replace every "owner" pointer in your program with a Reference
Counting Smart Pointer (like the one on mine or many other folks'
web pages).
When children die, they destroy the RCSP to their parent. When the
last of these RCSPs dies, the RCSP itself calls the destructor of
the parent.
The parent can neither destroy itself or be destroyed by the outside
clients (such as the User). All attempts to destroy things route to
the children, and the parent is destroyed when the last one dies.
You have confused windows GUI "parent" with "owner object". In this
case the parent window is an inferior object to the children, and
only exists to support them.
If your UI goals include the concept of an open parent with no
children, add a Singleton to the program whose only job is to keep
one more RCSP lock on the parent. If the User selects the Close
option, nuke each child and then this Singleton.
--
Phlip at politizen dot com (address munged)
======= http://users.deltanet.com/~tegan/home.html =======
-- "Probably one of the toughest times in anyone's life...
is when you have to kill someone you love...
because they are the Devil" - Emo Phillips --
Technical Details
Message-ID:
<7dhih3$8rt@chronicle.concentric.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<mKXK2.8237$_47.7309@news2.giganews.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f79ee,91ef7cab2cf22d X-Google-Attributes: gidf79ee,public X-Google-Thread: 109fba,91ef7cab2cf22d X-Google-Attributes: gid109fba,public X-Google-Thread: 10d4ba,91ef7cab2cf22d X-Google-Attributes: gid10d4ba,public X-Google-Thread: 114592,91ef7cab2cf22d X-Google-Attributes: gid114592,public From: "Phlip" <phlip@politizen.com> Subject: Re: Destructing encapsulated objects Date: 1999/03/26 Message-ID: <7dhih3$8rt@chronicle.concentric.net> X-Deja-AN: 459579890 References: <mKXK2.8237$_47.7309@news2.giganews.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Organization: Concentric Internet Services Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++,it.comp.lang.c++