Opened 10 years ago

#7106 new Bugs

multi_array::resize switches to default-constructed allocator

Reported by: bmerry@… Owned by: Ronald Garcia
Milestone: To Be Determined Component: multi_array
Version: Boost Development Trunk Severity: Problem
Keywords: allocator Cc:

Description

I've run into this with 1.46 but eyeballing the code on trunk it doesn't seem to have changed.

The implementation of resize starts like this:

    // build a multi_array with the specs given
    multi_array new_array(ranges,this->storage_order());

However, no allocator is passed to this constructor. If *this has a non-default allocator, then it will not get used to allocate the resized storage, and furthermore, the default allocator in new_array will get swapped in to *this, wiping out the custom allocator.

This can be fixed by changing this line to

    multi_array new_array(ranges,this->storage_order(),allocator_);

so that the new array uses the same allocator as the current one.

I'll attach a test case that demonstrates the issue by using an allocator class where instances are named and report the allocations they make (I'm using a more complex version of this to track memory usage, which is where I hit the bug). It currently reports

Allocated 17 bytes [name = vector]
Allocated 0 bytes [name = multi_array]
Allocated 15 bytes [name = default]

but with the fix applied it reports

Allocated 17 bytes [name = vector]
Allocated 0 bytes [name = multi_array]
Allocated 15 bytes [name = multi_array]

It is the final line where the resize is taking place.

Attachments (1)

multi_array-alloc.cpp (1.6 KB ) - added by bmerry@… 10 years ago.

Download all attachments as: .zip

Change History (1)

by bmerry@…, 10 years ago

Attachment: multi_array-alloc.cpp added
Note: See TracTickets for help on using tickets.