# Upgrade of StableBTreeMap created dynamically

**URL:** <https://forum.dfinity.org/t/upgrade-of-stablebtreemap-created-dynamically/28278>\
**Category:** Developers\
**Created:** [March 9, 2024, 2:05pm UTC](https://forum.dfinity.org/t/upgrade-of-stablebtreemap-created-dynamically/28278 "2024-03-09T14:05:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 9, 2024, 2:05pm UTC](https://forum.dfinity.org/t/upgrade-of-stablebtreemap-created-dynamically/28278/1 "2024-03-09T14:05:53Z")

</div>

In case one would like to create `StableBTreeMap` on the fly, e.g.

```auto
fn get_memory_for_something(index: u8) -> Memory {
    MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new( index)))
}

pub fn init_stable_something(index: u8) -> Something {
    StableBTreeMap::init(get_memory_for_something(index))
}

```

Is there a better pattern than holding references to the generated `MemoryId` in another `StableVec` or similar stable structure to ensure the memory of the canister can be upgraded without loosing references to the memory?

---

<div class="post-metadata">

**Author:** ![ielashi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ielashi/32/15704_2.png) [@ielashi](https://forum.dfinity.org/u/ielashi)\
**Post date:** [March 11, 2024, 11:50am UTC](https://forum.dfinity.org/t/upgrade-of-stablebtreemap-created-dynamically/28278/2 "2024-03-11T11:50:13Z")

</div>

`StableBTreeMap`, and stable structures in general, should _not_ be created dynamically - that’s not what they are designed for. What’s the use-case?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 11, 2024, 12:13pm UTC](https://forum.dfinity.org/t/upgrade-of-stablebtreemap-created-dynamically/28278/3 "2024-03-11T12:13:26Z")

</div>

> [@ielashi](#):
>
> should _not_

Why not?

Is that documented somewhere?

Use case is similar to the one I shared in the other [thread](https://forum.dfinity.org/t/stable-structures-memoryid-u8/28250/3).
