473,799 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_p tr).
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_thi s():
template enable_shared_f rom_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_s hared_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 1962
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_thi s():
template enable_shared_f rom_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_s hared_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_p tr 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::intrusiv e_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
9059
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 type:
9
2310
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 CMyClass()); //???? Required ??????????????//
75
5489
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 been a significant decline in the number of students opting to take courses in C++. I just want to let people know what I believe are the biggest obstacles to C++ language acquisition, and what aspects of the language make it less appealing than...
7
2971
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 shared_ptr. one thread is meant to pass the shared_ptr to another after its processing. So when a message arrives i convert it into a class of my own called 'Msg' and i put the 'Msg' object pointer into a shared_ptr and put into the other thread's...
8
1284
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 you can take items from and put items into of different sizes. The reason I want to know is I am about to try my hands at learning classes and need to do it in a way I can grasp - ala gaming concepts (see inventory example above.) Thanks in...
14
6658
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 it. Specifically, part 1 is obvious to most anyone. Part 2 is isomorphic to part 1, yet behaves differently. If a shared_ptr is const, should it really allow non-const dereferences? Thanks,
4
3964
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
5
3204
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
5
7664
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 obj::foo(shared_ptr<>&); and shared_ptr<>& obj::foo2(); vs. shared_ptr<obj::foo2();
0
9686
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...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
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
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.