Opened 9 years ago

Closed 9 years ago

#9166 closed Bugs (fixed)

Memory leak when copying empty boost::container::vector

Reported by: onlyone@… Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: container
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

Consider the following simple program:

#include <boost/container/vector.hpp>

int main() {
    while (true) {
        boost::container::vector<int> vec;
        boost::container::vector<int> vec2(vec);
    }
}

With Boost 1.54.0, this leaks an unbounded amount of memory. The copy contructor of boost::container::vector performs an allocation (which in this case asks for 0 bytes), and sets the capacity of the new vector the size of this allocation (0 elements), and since the capacity is 0, the destructor of vec2 does not deallocate this memory.

A simple patch is attached which disables allocation when the requested size is 0. This may not be the optimal solution (for example, there might be code that subsequently reads from m_start and expects it to contain a valid value (I have not yet studied the code sufficiently to know)). Even so, this is a major issue, and so I am reporting it even without a fully tested patch.

Evan Wallace

Attachments (1)

container_leak.patch (559 bytes ) - added by onlyone@… 9 years ago.
Patch that disables allocation when space for 0 elements is needed.

Download all attachments as: .zip

Change History (3)

by onlyone@…, 9 years ago

Attachment: container_leak.patch added

Patch that disables allocation when space for 0 elements is needed.

comment:1 by anonymous, 9 years ago

Summary: Memory when copying empty boost::container::vectorMemory leak when copying empty boost::container::vector

comment:2 by Ion Gaztañaga, 9 years ago

Resolution: fixed
Status: newclosed

(In [85964]) Default initialization for vector-like containers Complexity guarantees for associative container constructors and ordered input ranges Added benchmark for associative containers Fixes #9166

Note: See TracTickets for help on using tickets.