Skip to content

Memory leaks in BTreeMap with allocator_api enabled #106203

Open
@fuyufjh

Description

@fuyufjh

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=23d67c52988b3be4eb1ddb6373da124d

fn main() {
    let allocator = AllocWrapper(Arc::new(std::alloc::System));

    println!("count = {}", Arc::strong_count(&allocator.0));
    let mut btree_map: BTreeMap<i32, i32, _> = BTreeMap::new_in(allocator.clone());
    println!("count = {}", Arc::strong_count(&allocator.0));
    btree_map.insert(1, 1);
    println!("count = {}", Arc::strong_count(&allocator.0));
    drop(btree_map);
    println!("count = {}", Arc::strong_count(&allocator.0)); // expect 1 got 2!
}

I expected to see this happen:

The reference count of the Arc should reduce to 1, so that it can be released correctly.

Instead, this happened:

The reference count of the Arc remains 2, so it will never be released.

Meta

Build using the Nightly version: 1.68.0-nightly

(2022-12-26 88c58e3c2c097ebffac4)

with

#![feature(btreemap_alloc)]
#![feature(allocator_api)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions