473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

about shared_ptr

Hello,

in 2 other threads I had questions partly related to shared_ptr.

I changed my normal pointers to a class to shared_ptr. (i.e.
boost::shared_ptr).
I thought, the use of shared_ptr is more save. But after this my program
crashed by several reasons (see below). But maybe there would be an easy
solution which I cannot see now.

1) I mixed normal pointers and shared _ptr at some places. This is very
ugly, for example:

class A;

shared_ptr<A> a = new A;
A * p = a.this();
{
shared_ptr<A> a2 = p;
} // the object will be deleted here!!!
a->f(); // crash?
Question: Is it possible to make that a shared_ptr can only be initialised
by another shared_ptr or by the new expression?
2) shared_from_this():
template enable_shared_from_this cannot be used in a class with 2 instances
of the same base class?

In my hierarchy, I have the same base class twice in one object
(non-virtual). As this base class has boost::enable_shared_from_this<> as
Base again, I cannot compile (ambigous...).

Maybe I could circumvent this by making one Baseclass a virtual base class.
But wasn't there a restriction by using virtual base classes? (As far as I
can remember it was something with RTTI or with casts).

I know that 2 base classes is not a good idea, but there was a problem by
using virtual inheritance.

Greetings
Ernst


Jul 22 '05 #1
4 1941
On Thu, 8 Jan 2004 18:56:45 +0100, "Ernst Murnleitner"
<mu******@awite.de> wrote:
Hello,

in 2 other threads I had questions partly related to shared_ptr.

I changed my normal pointers to a class to shared_ptr. (i.e.
boost::shared_ptr).
I thought, the use of shared_ptr is more save. But after this my program
crashed by several reasons (see below). But maybe there would be an easy
solution which I cannot see now.

1) I mixed normal pointers and shared _ptr at some places. This is very
ugly, for example:

class A;

shared_ptr<A> a = new A;
Don't do the above - the code won't compile on a conforming compiler.
Instead, do:

shared_ptr<A> a(new A);
A * p = a.this();
{
shared_ptr<A> a2 = p;
} // the object will be deleted here!!!
a->f(); // crash?
Question: Is it possible to make that a shared_ptr can only be initialised
by another shared_ptr or by the new expression?
No, but using a conforming compiler prevents most mistakes (due to the
explicit T* constructor). Initializing a shared_ptr with a pointer
that hasn't just been newed in the same expression is almost always a
mistake, but if you know that, it's a hard mistake to make. Unless
your compiler has a bug that enables the implicit conversion, in which
case it is quite an easy mistake.
2) shared_from_this():
template enable_shared_from_this cannot be used in a class with 2 instances
of the same base class?

In my hierarchy, I have the same base class twice in one object
(non-virtual). As this base class has boost::enable_shared_from_this<> as
Base again, I cannot compile (ambigous...).
Right.

Maybe I could circumvent this by making one Baseclass a virtual base class.
But wasn't there a restriction by using virtual base classes? (As far as I
can remember it was something with RTTI or with casts).
Virtual inheritence has a small amount of overhead associated with it,
but no other problems that I can think of (other than having to
initialize the virtual base in the most derived class).
I know that 2 base classes is not a good idea, but there was a problem by
using virtual inheritance.


You should use virtual inheritence unless you really want two copies
of the base class (which I doubt).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
> >shared_ptr<A> a = new A;

Don't do the above - the code won't compile on a conforming compiler.
Instead, do:

shared_ptr<A> a(new A);


I think, in most cases, shared_ptr<A> a(new A) is not applicable, as
pointers are mostly used, if the class has to be constructed at runtime. If
I could make the initialisation this way, I would not need shared_ptr at
all:

In the class I defined a member variable
shared_ptr<A> a;

In the constructor, depending on the configuration stored in a data file:
a = new A;

Maybe I had to use
a = shared_ptr<A> (new A);
for compilers other than gcc 2.95?



Jul 22 '05 #3
On Fri, 9 Jan 2004 13:11:56 +0100, "Ernst Murnleitner"
<mu******@awite.de> wrote:
>shared_ptr<A> a = new A;


Don't do the above - the code won't compile on a conforming compiler.
Instead, do:

shared_ptr<A> a(new A);


I think, in most cases, shared_ptr<A> a(new A) is not applicable, as
pointers are mostly used, if the class has to be constructed at runtime. If
I could make the initialisation this way, I would not need shared_ptr at
all:

In the class I defined a member variable
shared_ptr<A> a;

In the constructor, depending on the configuration stored in a data file:
a = new A;

Maybe I had to use
a = shared_ptr<A> (new A);
for compilers other than gcc 2.95?


Yes, you do. This bug in 2.95 makes using shared_ptr unnecessarily
hazardous!

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
boost::shared_ptr did not work, as I needed a pointer to "this" already in
the constructor. As I found no way to fix this, I finally used
boost::intrusive_ptr.

As my classes are inherited from a base class, intrusive_ptr was a very good
solution. I can use the address operator for assignment to the
intrusive_ptr, and I can also assign "this" to the pointer at any time.
(Reference counts are incremented/decremented in the base class).

Greetings
Ernst


Jul 22 '05 #5

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

Similar topics

6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
8
by: Devon Null | last post by:
I was wondering if there is a container (i.e. a vector) that can hold data structures of differing sizes. I was thinking of something along the lines of a container of classes. Think of a backpack...
14
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
5
by: .rhavin grobert | last post by:
are there any pro's and con's in using references to shared_ptr<>'s ? example: __________________________________________________ void obj::foo(shared_ptr<>); vs. void...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.