Getting to grips with boost::shared_ptr

Hi there,

I have a class called Sounds which creates a number of Sound objects
in its constructor and adds them to a local std:map variable. I also
define an enum in this class which I use as the key with respect to
the map. This class therefore acts as a look-up for all the sounds I
wish to use.

I also have a Player class which has a Play(Sound&) function. This
function stops the currently playing sound and plays the new sound
supplied by the argument. I also have a Stop() function which stops
the current sound. I use a Sound* local variable to keep track of the
current sound.

The question is, how do I know that I shouldn't be using a shared_ptr
instead of this pointer? - a design change which would also require
storing shared_pts in the map previously mentioned. For example, I can
crash my program by deleting my Sounds object before calling
Player.Stop(). Using shared_ptrs would avoid this crash. However, this
is a reckless way of producing a crash, are there more innocent ways
in which it could occur for my Player class?

For me, it feels that my Player class does not take ownership of a
sound object; it simply keeps track of it and therefore I use a simple
pointer. Is this the correct mentality with regard to whether I should
be using a shared_ptr or a simple pointer?

Thanks very much for your help,

Barry
0
Barry
3/24/2010 6:32:59 PM
comp.lang.c++ 49102 articles. 1 followers. Post Follow

2 Replies
290 Views

Similar Articles

[PageSpeed] 43

* Barry:
> Hi there,
> 
> I have a class called Sounds which creates a number of Sound objects
> in its constructor and adds them to a local std:map variable. I also
> define an enum in this class which I use as the key with respect to
> the map. This class therefore acts as a look-up for all the sounds I
> wish to use.
> 
> I also have a Player class which has a Play(Sound&) function. This
> function stops the currently playing sound and plays the new sound
> supplied by the argument. I also have a Stop() function which stops
> the current sound. I use a Sound* local variable to keep track of the
> current sound.
> 
> The question is, how do I know that I shouldn't be using a shared_ptr
> instead of this pointer? - a design change which would also require
> storing shared_pts in the map previously mentioned. For example, I can
> crash my program by deleting my Sounds object before calling
> Player.Stop(). Using shared_ptrs would avoid this crash. However, this
> is a reckless way of producing a crash, are there more innocent ways
> in which it could occur for my Player class?
> 
> For me, it feels that my Player class does not take ownership of a
> sound object; it simply keeps track of it and therefore I use a simple
> pointer. Is this the correct mentality with regard to whether I should
> be using a shared_ptr or a simple pointer?

It's OK.

Consider that the same problems that you envision exist with nearly all kinds of 
objects in C++. To avoid the /possibility/ of dangling pointers every object 
would have to be allocated dynamically and managed through smart pointers and/or 
garbage collection. If that is what one wants then some other language, e.g. 
Java or C#, is a more practical answer  --  it's not the C++ way.


Cheers,

- Alf
0
Alf
3/25/2010 9:36:48 AM
Barry <bg_ie@yahoo.com> wrote in news:efac0ea9-d335-42da-8b92-
cf773ad3718e@q23g2000yqd.googlegroups.com:

> Hi there,
> 
> I have a class called Sounds which creates a number of Sound objects
> in its constructor and adds them to a local std:map variable. I also
> define an enum in this class which I use as the key with respect to
> the map. This class therefore acts as a look-up for all the sounds I
> wish to use.
> 
> I also have a Player class which has a Play(Sound&) function. This
> function stops the currently playing sound and plays the new sound
> supplied by the argument. I also have a Stop() function which stops
> the current sound. I use a Sound* local variable to keep track of the
> current sound.
> 
> The question is, how do I know that I shouldn't be using a shared_ptr
> instead of this pointer? - a design change which would also require
> storing shared_pts in the map previously mentioned. For example, I can
> crash my program by deleting my Sounds object before calling
> Player.Stop(). Using shared_ptrs would avoid this crash. However, this
> is a reckless way of producing a crash, are there more innocent ways
> in which it could occur for my Player class?
> 
> For me, it feels that my Player class does not take ownership of a
> sound object; it simply keeps track of it and therefore I use a simple
> pointer. Is this the correct mentality with regard to whether I should
> be using a shared_ptr or a simple pointer?

Yes, the only thing shared_ptr does is to keep the object alive until it is 
not needed any more. If you are not deleting any Sound anyway before the 
end of the program, then you do not need shared_ptr. OTOH if you foresee 
that you might want sometimes to delete a Sound in the middle of the 
program, for example by replacing it with an updated version loaded from 
disk, then shared_ptr would probably be the simplest way to achieve this 
feature.

cheers
Paavo
0
Paavo
3/26/2010 7:54:30 AM
Reply:

Similar Artilces:

gets() fgets() with subdivide.c tilepack.c squarect.c
I am looking to use Ken Stephenson's CirclePack program (http://www.math.utk.edu/~kens/) with some tiling programs written by Cannon, Floyd, Parry. The programs I want to use are subdivide.c tilepack.c squarect.c . They are available from http://www.math.vt.edu/people/floyd/research/software/subdiv.html. I tried compiling them on my Redhat 9 box, eg cc subdivide.c. When I do so I get errors like "In function `Readtilingforvertex': : the `gets' function is dangerous and should not be used." I checked the FAQ and is says gets() is BAD and I should use fgets() instead....

C++ has Boost libraries, C?
Hello, is there any library for C as Boost is for C++? thanks in advance, g said: > Hello, > > is there any library for C as Boost is for C++? There are lots of libraries available with C bindings. What functionality are you after? -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Richard Heathfield wrote: > g said: > > is there any library for C as Boost is for C++? > > There are lots of libraries available with C bindings. True, but that doesn't answer the question....

tr1::shared_ptr and boost::shared_ptr
What was the justification for the tr1 shared_ptr not to allow a custom deleter like the boost::shared_ptr? --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ] Axter wrote: > What was the justification for the tr1 shared_ptr not to allow a custom > deleter like the boost::shared_ptr? > There is none. It's part of the T...

#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"?
Is there a general rule/convention for when to use angle brackets and when to use quotes in #include statements? Is the angle bracket reserved for "system" header files (e.g. standard library headers), or is it for general "stable" include files? What about the grey areas in between these and actual application code, e.g. headers for a library that is under my control but external to the project being compiled? I know both styles work, and in the case of the Boost headers I've seen both of them used in example code, I'm just wondering if one is more "c...

lack of implicit casting to T* and boost::shared_ptr::get()
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of: http://www.boost.org/libs/smart_ptr/shared_ptr.htm) Can anyone say specifically what about the implicit conversion would be dangerous? I can think of a few things that would require care about usage of smart pointers with implicit conversions, but nothing that I would consider 'error-prone'. In any language, there are implications and C++ certainly has its fair share of other implicit behavio...

How to get BOOST working on XP and Visual C++ 2005
My experiences with BOOST on Windows XP and Visual C++ 2005 I'm new to Python. I built software in more than ten other computer languages. I'm not sure if that is not a handicap, when reading documentation of a system like BOOST. However: It took me more than four full working days to get a Python/C/C++ 'hello world!' program to work. There is a lot of documentation, but somehow it did not 'click' with my previous experience. I think the doc was written by very, very capable C++ and Python programmers, who forgot how the world was before they got involved with Python. ...

get the shape of a numpy ndarray in C++ code [boost.python]
[sorry is half a post appeared earlier. Bloody Google groups...] Hello, I'm trying to use a numpy array in C++ (win2000) using boost.python. Test code: void test( numeric::array& nsP) { object shape = nsP.getshape(); int rows = extract<int>(shape[0]); int cols = extract<int>(shape[1]); } At first, running it in Python got me this message: ArgumentError: Python argument types in d3d.wr_conn(numpy.ndarray) did not match C++ signature: wr_conn(class boost::python::numeric::array {lvalue}) I fixed this using this line: numeric::array::set_...

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta >...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

Boost python : get the shape of a numpy ndarray in C++ code.
Hi there. I'm strugling here with some boost python code (damn I hate C++) : All I want to do is to initialize the content of an array with a numpy ndarray parameter. I have this, which actually works. But I want to add some kind of data check such as : * is array two dimensional ? * are the dimensions corresponding to map's width / height ? * is array field with floats or ints ? void Layer::set_potentials (numeric::array& array) { for (int h=0; h<map->height; h++){ for (int w=0; w<map->width; w++){ units[w+h*map->width]->potential = extract<flo...

How do you get the username from the user_id (in c/c++)
I am making a list of users and the amount of space they are using on a device. I traverse the file system and get a list of filenames.. I use these filenames and do an fstat on each... fstat (on my MAC OS X) machine returns a structure that contains the filesize and also the user_id. (This is a c program I'm writing.) My question is how do I convert the user_id into the users name? (which should be a null terminated string.) What is the function call? TIA. B. On Fri, 10 Dec 2004, JustSomeGuy wrote: > My question is how do I convert the user_id into the users name? > (which s...

What are the differences between std::shared_ptr and boost::shared_ptr?
What are the differences between std::shared_ptr and boost::shared_ptr? I just know they are, but don't know where. Thanks. * Kai Wen, on 14.09.2010 04:02: > What are the differences between std::shared_ptr and boost::shared_ptr? > I just know they are, but don't know where. > Thanks. Mostly none (except for the namespace), because std::shared_ptr was adopted from boost::shared_ptr. There may be a difference with respect to custom deleter replacement, and perhaps with respect to conversions and perhaps also iostream operator<< support, that is, that pe...

How to get signed zeros in C++/C?
I don't find how to express signed zeros in C++/C. Does anybody know what is the proper way to do so? Are they just -0.0 and +0.0? How to distinguish signed zeros from true zeros. http://en.wikipedia.org/wiki/Signed_zero Regard, Peng Peng Yu <pengyu.ut@gmail.com> writes: > I don't find how to express signed zeros in C++/C. Does anybody know > what is the proper way to do so? Are they just -0.0 and +0.0? How to > distinguish signed zeros from true zeros. > > http://en.wikipedia.org/wiki/Signed_zero AFAIK there is no literal for those. You need to ...

Just getting started in C++, C+
Thanks in advance to all of you, Your recommendations: What kind of instruction and software are needed to learn and do useful programming using C+, C++ ? I would like to access and use the windows graphics environment. SRabin, > What kind of instruction and software are needed to learn and do useful > programming using C+, C++ ? There is nothing like C+. A good tutorial: http://www.cplusplus.com/doc/tutorial/ > I would like to access and use the windows graphics environment. Free: DevC++ (core c++, that black command line window) + WxWidgets (for graphical user interfaces) C...

Web resources about - Getting to grips with boost::shared_ptr - comp.lang.c++

Resources last updated: 2/3/2016 1:46:32 AM