# How to deallocate userdata?

**URL:** <https://qa.fmod.com/t/how-to-deallocate-userdata/21120>\
**Category:** FMOD Engine\
**Created:** [January 18, 2024, 9:51pm UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120 "2024-01-18T21:51:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Speak4Erase](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Speak4Erase](https://qa.fmod.com/u/Speak4Erase)\
**Post date:** [January 18, 2024, 9:51pm UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/1 "2024-01-18T21:51:12Z")

</div>

How do I deallocate any heap allocated userdata I set?

```cpp
// bank used here as an example, but this could be anything
Bank* bank;

UserData* userData = new UserData;
// how do I free this?
bank->setUserData(userData);

```

All the examples I can find involve setting userdata to a value on the stack, nothing that uses `new` or `malloc`.

---

<div class="post-metadata">

**Author:** ![aishi1](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@aishi1](https://qa.fmod.com/u/aishi1)\
**Post date:** [January 19, 2024, 10:57am UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/2 "2024-01-19T10:57:53Z")

</div>

Hello, if you don’t have access to `userData` when freeing, you could retrieve the pointer with `Bank::getUserData` and call `delete` on it after casting it back to `UserData *`.

Something like this should work:

```cpp
UserData *data;
FMOD_RESULT result = bank->getUserData((void **)&data);
if (result == FMOD_OK)
{
    delete data;
}
else
{
    // error
}

```

---

<div class="post-metadata">

**Author:** ![Speak4Erase](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Speak4Erase](https://qa.fmod.com/u/Speak4Erase)\
**Post date:** [January 19, 2024, 9:58pm UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/3 "2024-01-19T21:58:35Z")

</div>

How do I know when a bank (or any other object with associated userdata) is about to be released, so I can also deallocate the userdata associated with it?

---

<div class="post-metadata">

**Author:** ![aishi1](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@aishi1](https://qa.fmod.com/u/aishi1)\
**Post date:** [January 20, 2024, 12:26am UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/4 "2024-01-20T00:26:49Z")

</div>

You could add a callback with [`FMOD::System::setCallback`](https://www.fmod.com/docs/2.00/api/studio-api-system.html#studio_system_setcallback) that checks for the callback type `FMOD_STUDIO_SYSTEM_CALLBACK_BANK_UNLOAD`.

This callback passes the handle of the bank where you can get and deallocate the user data.

Unless there’s a strong reason not to, I’d probably just make a class that wraps the bank + userdata and handles unloading/deallocating without needing callbacks.

---

<div class="post-metadata">

**Author:** ![Speak4Erase](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Speak4Erase](https://qa.fmod.com/u/Speak4Erase)\
**Post date:** [January 20, 2024, 12:31am UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/5 "2024-01-20T00:31:09Z")

</div>

Alright! I’ll try that, although I am a little worried about potential use after frees.

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [January 21, 2024, 9:39pm UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/6 "2024-01-21T21:39:32Z")

</div>

Besides what @aishi1 has already mentioned with regards to knowing when to release user data - to safely deallocate user data, set the user data to `nullptr` or equivalent with `setUserData()`, and then free your memory. User data isn’t touched by FMOD, except for when using [`FMOD_STUDIO_BANK_INFO`](https://www.fmod.com/docs/2.02/api/studio-api-system.html#fmod_studio_bank_info), where a non-zero `userdatalength` will cause FMOD to copy the data from your pointer to internal memory.

---

<div class="post-metadata">

**Author:** ![Speak4Erase](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Speak4Erase](https://qa.fmod.com/u/Speak4Erase)\
**Post date:** [February 1, 2024, 8:17pm UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/7 "2024-02-01T20:17:25Z")

</div>

So far I’ve gotten this strategy to (mostly) work, until I tried deallocating userdata attached to event descriptions. My code roughly looks like this:

```auto
FMOD_RESULT deallocate_bank(Bank bank) {
    void* userdata;
    bank.get_userdata(&userdata);

    // deallocate the userdata if it is not null
    if userdata {
       delete (InternalBankUserdata*) userdata;
       bank.set_userdata(NULL);
    }

    std::vector<EventDescription> list = get_event_list(bank);
    for (EventDescription event : list) {
        event.get_userdata(&userdata);

        // deallocate the userdata if it is not null
        if userdata {
           delete (InternalEventUserdata*) userdata;
           event.set_userdata(NULL);
        }
    }

    return FMOD_OK;
}

```

`deallocate_bank` is called from a system callback, per aishi1’s solution.  
My `get_event_list` function uses `Bank::get_event_count` and `Bank::get_event_list` to get the list of all event descriptions. When I call either function though, they just return `FMOD_ERR_NOTREADY`. What should I do?

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [February 6, 2024, 10:25pm UTC](https://qa.fmod.com/t/how-to-deallocate-userdata/21120/8 "2024-02-06T22:25:18Z")

</div>

> [@Speak4Erase](#):
>
> My `get_event_list` function uses `Bank::get_event_count` and `Bank::get_event_list` to get the list of all event descriptions. When I call either function though, they just return `FMOD_ERR_NOTREADY`. What should I do?

At the point where `FMOD_STUDIO_SYSTEM_CALLBACK_BANK_UNLOAD` fires, the bank has already been unloaded, and all resources freed. As a result, you won’t be able to iterate over the bank’s event descriptions from the callback - you’ll need to do that before unloading the bank.
