C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Referencecounting and Handles best practices

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Steven T. Hatton
Guest





PostPosted: Sun Jun 19, 2005 11:52 am    Post subject: Referencecounting and Handles best practices Reply with quote



Buried in the discussion below is the following question:

Is there a good source that explores this topic fairly exhaustively? I'd
really like to know the answer to that question, and what that source might
be.

I'm trying to establish good practices for using "smart pointers". I'm not
really sure what established conventions, if any, exist within the C++
community. There seem to be different broad categories of strategy used to
deal with issues of 'automatic' memory management. I believe the general
problem could be stated as: how do we ensure a dynamically allocated object
is destroyed when no longer needed, and not destroyed in such a way as to
leave an opportunity for subsequent attempts to access the memory it once
held as if it still existed?

The various approaches that I perceive are:

*Unique ownership, e.g., std::auto_ptr<>

*Shared ownership using external reference counting, e.g.,
boost::shared_ptr<>
**with shared modification.
**with copy on modify.

*Shared ownership using internal reference counting, e.g.,
boost::intrusive_ptr<>
**with shared modification.
**with copy on modify. (I'm not sure how to do this)

What I would like is some way to establish the behavior in baseclasses from
which I could inherit with a minimum of burden placed on classes derived
from these.

The advantages of using the shared pointer approach over the intrusive
pointer approach seem to be:
*There is less (or no) need to design the shared object with special
functionality to support the sharing.
*It's easier to support copy on modify (implicit sharing).
*The design is applicable to a broader range of objects

The disadvantages seem to be:
*The shared object does not carry an internal reference count, so it's
easier to lose track of the reference count.
*It may take more work per class to implement the sharing mechanism.

I'm not sure how carfully explored this area of C++ programming is. Is
there a good source that explores this topic fairly exhaustively? This
really is proving to be a stumbling block for me. It seems like the kind
of thing you really need go get right early on in the design process,
because much else will be built upon it. The best example I can think of
where a particular design choice has pretty much been locked into the
product is OpenSceneGraph which uses an approach similar to the
boots::intrusive_ptr<>. It's certainly useable (and useful), but I'm not
sure if it's ideal.

http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs/classosg_1_1ref__ptr.html
http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs/classosg_1_1Referenced.html

Another example where shared data models are used is in the Qt library:
http://doc.trolltech.com/3.3/shclass.html

I read through the code for OSG's classes a while back, but haven't looked
very deeply into Qt's implementation(s). I also have Accelerated C++
opened to Chapter 14, sitting in my lap as I type.
--
Regards,
Steven

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
meadori
Guest





PostPosted: Tue Jun 21, 2005 10:09 am    Post subject: Re: Referencecounting and Handles best practices Reply with quote



Quote:
Is there a good source that explores this topic fairly exhaustively?
Although I am not sure I would classify them as completely exhaustive I

enjoyed the coverage of this topic in:
1.) Chapter 7 of 'Modern C++ Design' (judging by your post you will
probably find this selection particularly interesting)
2.) Chapter 20 of 'C++ Templates: The Complete Guide'


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.