473,769 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tell me again what's wrong with intrusive pointers?

It's my understanding that intrusive pointers are frowned upon. For example
this is from the boost::intrusiv e_ptr documentation: "As a general rule, if
it isn't obvious whether intrusive_ptr better fits your needs than
shared_ptr, try a shared_ptr-based design first."

This tells me that intrusive pointers are faster and leaner than any of the
other alternative other than raw pointers.
http://www.boost.org/libs/smart_ptr/smarttests.htm

To my way of thinking they are easier to use when working with recursive
data structures (trees). I was trying to use boost::shared_p tr because of
the comment quoted above, and similar comments I've read elsewhere. It
often makes sense when constructing nodes to pass the parent as a
parameter, and add `this' to the parent as a child. I tried doing that
with a shared_ptr implementation using boost::enable_s hared_from_this .
When I executed the code, it threw a bad_weak_ptr exception, which traced
back to where I was trying to pass a shared_ptr to `this'. I assume the
problem is that I cannot use that feature until the object is fully
constructed and an owner is established.

If the argument against intrusive pointers has to do with not wanting to
mess with the internals of the class being pointed to, that seems obviated
by the use of boost::enable_s hared_from_this . AFAIK, using shared_ptr it
is possible to establish two independent reference counts which is a
guaranteed path to disaster. That can't happen with intrusive pointers.
What are the reasons for favoring shared_ptr over intrusive_ptr?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 30 '06 #1
2 2922
* Steven T. Hatton:
What are the reasons for favoring shared_ptr over intrusive_ptr?
One reason is that intrusive reference counting (I don't know about the
Boost version) in general makes it possible to get away with using raw
pointers in client code. And that temptation is /very/ very strong:
it's the micro-optimization attractor, which for programmers is stronger
than the sex drive, even stronger than death-avoidance. Another reason
is that it necessitates using at least two kinds of smart pointers for
essentially the same conceptual-level purpose: non-intrusive smart
pointers for those classes that can't be outfitted with internal
reference counting, and intrusive ones, and for this, more is less.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 30 '06 #2
Alf P. Steinbach wrote:
* Steven T. Hatton:
>What are the reasons for favoring shared_ptr over intrusive_ptr?

One reason is that intrusive reference counting (I don't know about the
Boost version) in general makes it possible to get away with using raw
pointers in client code.
With boost::shared_p tr you can do that, so in this case that doesn't apply.
And that temptation is /very/ very strong:
it's the micro-optimization attractor, which for programmers is stronger
than the sex drive, even stronger than death-avoidance. Another reason
is that it necessitates using at least two kinds of smart pointers for
essentially the same conceptual-level purpose: non-intrusive smart
pointers for those classes that can't be outfitted with internal
reference counting, and intrusive ones, and for this, more is less.
With OpenSceneGraph there is a galactic base class (like a universal base
class, but not as inclusive) called osg::Referenced . If you want to use
the corresponding osg::ref_ptr, you simply derive from referenced and have
at it. There are things to be aware of such as the consequences of
deriving from osg::Referenced and not making the destructor protected, and
then putting an instance on the stack.

The problem of introducing two variants of smart pointers doesn't seem very
big in this situation. There are two kinds of things I'd be keeping track
of using the intrusive reference counting, nodes and node data. I probably
won't need shared pointers beyond those.

I can probably make the shared_ptr work if I forego trying to append the new
node to its parent in the constructor. I probably should learn to work
with it, if for no other reason, because other people seem to think it's
the way to go.

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 30 '06 #3

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

Similar topics

3
1626
by: Jonathan | last post by:
Rather than me explain the problem, read this exchange of emails between my web host and myself, then tell me what I can tell them! I had a problem with their admin site all day, and after me saying "can't log in", and them saying "nothing wrong", I sent the following: ------------------------------------------------------ > Found it! Tried it with 3 browsers now (IE, Opera, Firefox), and the > result is that I've just discovered a...
303
17753
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
0
1450
by: Sabyasachi Basu | last post by:
Most C++ object serialization techniques are intrusive, that is they require all serializable classes need to have functions to stream in and stream out their contents. I came across "non-intrusive serialization" while going through the tutorial on Robert Ramey's serialization library for Boost. However, it is not clear to me how to implement serialization without the cooperation of the classes being serialized. Can someone through...
9
4056
by: Marcin Kalicinski | last post by:
Hi everybody, I am creating an intrusive list ('next' and 'prev' pointers are stored within an object that is in a list). One method of doing that is to inherit all objects from some class that contains these pointers. However, it is unacceptable for my problem, because it limits the number of lists the object can be in to 1. I must have some objects in more than 1 list. Instead of inheriting, I use aggregation: class Node { //...
6
5426
by: Pradeep | last post by:
Hi, I am using intrusive pointers for my code but i am not much aquianted with them. I am using an example to illustrate my problem as i can't share the code. I have two clases ( say A and B) B is derived from A( B: public A) now i want to pass the this* pointer of the derived class(B) to a
1
1623
by: Pradeep | last post by:
Hi, I am working with Intrusive pointers in a DLL and I need to set the value of the intrusive pointer to an object to NULL from inside the method of that class. So here's the scenario. I have a class A which has a method del().
156
5899
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I dispose of it unless I do the following:
39
19646
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
6
1277
by: stork | last post by:
C++ is always evolving. Within C++, there tend to be two communities. There is the community pushing for performance features, getting ever closer to the metal, and then, there is the applications community. The applications community would, in general, prefer C++ to become more high level - adding things like garbage collectors, improved pointer safety, in short, to be a language useful for building applications that now go to the...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10198
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7392
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.