Upgrade of StableBTreeMap created dynamically

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

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?

1 Like

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

Why not?

Is that documented somewhere?

Use case is similar to the one I shared in the other thread.