Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Boost circular buffer

67 views
Skip to first unread message

bitrex

unread,
May 5, 2017, 1:34:57 PM5/5/17
to
Is it OK to use the templated circular buffer included in Boost to store
std::shared_ptrs to dynamically allocated objects?

The documentation says "One recommend alternative is the use of smart
pointers [1]. (Any container of std::auto_ptr is considered particularly
hazardous. [2] )"; however it doesn't seem entirely clear on whether a
resource held by a smart pointer within the container instead of a
regular ol' pointer will be released correctly if a block in the buffer
is overwritten.

Alf P. Steinbach

unread,
May 5, 2017, 2:16:28 PM5/5/17
to
It's very very very rare that a container doesn't properly destroy its
items. That's only done for very low level things, like C `realloc`.
When it's done in any halfway quality softeware it's documented.

In other words, you can trust the Boost containers to do the Right Thing.


Cheers & hth.,

- Alf

bitrex

unread,
May 5, 2017, 3:51:50 PM5/5/17
to
Thanks!

Chris Vine

unread,
May 6, 2017, 11:42:53 AM5/6/17
to
The text to which you refer is preceded in the documentation by the
sentences: "The circular_buffer should not be used for storing pointers
to dynamically allocated objects. When a circular buffer becomes full,
further insertion will overwrite the stored pointers - resulting in a
memory leak."

The point being made here is that when the buffer is full, additional
pushes to it will overwrite the oldest item, so that if the data items
are pointers to items allocated on free store and there is no other
pointer held for the item to be overwritten enabling it to be freed, you
will have a memory leak.

shared_ptr will certainly be safe. As regards how overwriting is dealt
with, the documentation says this:

'There was a discussion what exactly "overwriting of an element" means
during the formal review. It may be either a destruction of the
original element and a consequent inplace construction of a new
element or it may be an assignment of a new element into an old one.
The circular_buffer implements assignment because it is more
effective.'

Either approach is OK with shared_ptr.

Chris

Juha Nieminen

unread,
May 8, 2017, 6:10:49 AM5/8/17
to
bitrex <bit...@de.lete.earthlink.net> wrote:
> Is it OK to use the templated circular buffer included in Boost to store
> std::shared_ptrs to dynamically allocated objects?

The documentation is talking about storing raw pointers in the container.
std::shared_ptr is always safe to use in these containers.

woodb...@gmail.com

unread,
May 8, 2017, 12:00:44 PM5/8/17
to
It depends on the compiler and hardware though:
https://akrzemi1.wordpress.com/2017/05/05/the-gcc-bug-affects-you/


Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

woodb...@gmail.com

unread,
May 8, 2017, 12:24:11 PM5/8/17
to
On Monday, May 8, 2017 at 11:00:44 AM UTC-5, woodb...@gmail.com wrote:
> On Monday, May 8, 2017 at 5:10:49 AM UTC-5, Juha Nieminen wrote:
> > bitrex <bit...@de.lete.earthlink.net> wrote:
> > > Is it OK to use the templated circular buffer included in Boost to store
> > > std::shared_ptrs to dynamically allocated objects?
> >
> > The documentation is talking about storing raw pointers in the container.
> > std::shared_ptr is always safe to use in these containers.
>
> It depends on the compiler and hardware though:
> https://akrzemi1.wordpress.com/2017/05/05/the-gcc-bug-affects-you/
>
>

I heard about that bug on http://cppcast.com
.

Jorgen Grahn

unread,
May 9, 2017, 3:45:15 PM5/9/17
to
On Mon, 2017-05-08, woodb...@gmail.com wrote:
> On Monday, May 8, 2017 at 5:10:49 AM UTC-5, Juha Nieminen wrote:
>> bitrex <bit...@de.lete.earthlink.net> wrote:
>> > Is it OK to use the templated circular buffer included in Boost to store
>> > std::shared_ptrs to dynamically allocated objects?
>>
>> The documentation is talking about storing raw pointers in the container.
>> std::shared_ptr is always safe to use in these containers.
>
> It depends on the compiler and hardware though:
> https://akrzemi1.wordpress.com/2017/05/05/the-gcc-bug-affects-you/

Based on the URL, I'd say a compiler bug can break any code, including
"Hello, world".

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages