What you are seeing is most likely caused by your malloc or string implementation (most likely not a bug, just an observation that differs from what you would expect). If your strings are of variable size, memory will most likely grow due to heap fragmentation. This should not grow unbounded, since after the second pass through the loop, the chunk of memory occupied by the first strings should be freed.
To show that circular_buffer works as documented, try a more simple object like this:
int allocations = 0;
struct AllocCountingObj
{
AllocCountingObj()
{
++allocations;
}
AllocCountingObj(const AllocCountingObj&)
{
// still increment on copy-construct
++allocations;
}
~AllocCountingObj()
{
--allocations;
}
};
What compiler and STL implementation are you using and what are you using to view the memory use?
I am using the circular buffer container to hold up to 10000 std string objects. I am finding that once the buffer has reached max capacity, memory continues to be consumed as I push new strings in. Is there something I¢m missing? I stepped into the boost code and it does make a copy of the string using the std string operator so I would think it handles the string copy properly. The size of the strings are variable but I would expect the memory usage to be fairly stable when the buffer is full but that is not the case.
Here¢s basic usage
Definition:
boost::circular_buffer<std::string> m_message_buffer;
Initialization:
m_message_buffer.set_capacity (10000);
Usage:
void myClass::writeToBuffer (const char *message)
{
m_message_buffer.push_back (string (message));
}
Boost code:
void push_back(param_value_type item = value_type()) {
if (full()) {
if (empty())
return;
replace(m_last, item);
increment(m_last);
m_first = m_last;
} else {
m_alloc.construct(m_last, item);
increment(m_last);
++m_size;
}
}
void replace(pointer pos, param_value_type item) {
*pos = item;
#if BOOST_CB_ENABLE_DEBUG
invalidate_iterators(iterator(this, pos));
#endif
}
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users