# EventDescription and EventInstance caching in C++

**URL:** <https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103>\
**Category:** FMOD Studio\
**Created:** [August 14, 2013, 12:46pm UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103 "2013-08-14T12:46:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://qa.fmod.com/u/Kai)\
**Post date:** [August 14, 2013, 12:46pm UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/1 "2013-08-14T12:46:37Z")

</div>

Q1: Does FMOD Studio cache EventDescription objects internally or is it loading from disk every time I call System::getEvent()?

Q2: Does EventInstance support copy by value? For example, a sound plays with the following code:

```auto
FMOD::Studio::EventInstance eventInstance;
eventDescription.createInstance(&eventInstance);

FMOD::Studio::EventInstance copy = eventInstance;
copy.start();
```

However, when I used this trick, FMOD would crash when I called System::release(). Should I avoid copying and instead rely on allocating heap memory for long term EventInstance objects?

---

<div class="post-metadata">

**Author:** ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)\
**Post date:** [August 15, 2013, 12:17am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/2 "2013-08-15T00:17:32Z")

</div>

> [@](#):
>
> Q1: Does FMOD Studio cache EventDescription objects internally or is it loading from disk every time I call System::getEvent()?

Currently it caches them internally when you load the bank, so getEvent() doesn’t read anything from disk.

> [@](#):
>
> Q2: Does EventInstance support copy by value? For example, a sound plays with the following code:
> 
> ```auto
> FMOD::Studio::EventInstance eventInstance;
> eventDescription.createInstance(&eventInstance);
> 
> FMOD::Studio::EventInstance copy = eventInstance;
> copy.start();
> ```

You can copy EventInstance handles like that, but they will both refer to the same event instance internally. If you want more than one event instance, you need to call createInstance() multiple times.

> [@](#):
>
> However, when I used this trick, FMOD would crash when I called System::release(). Should I avoid copying and instead rely on allocating heap memory for long term EventInstance objects?

This shouldn’t cause a crash - sorry about that! We’ll look into it.

---

<div class="post-metadata">

**Author:** ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)\
**Post date:** [August 15, 2013, 12:48am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/3 "2013-08-15T00:48:23Z")

</div>

> [@ben](#):
>
> > [@](#):
> >
> > However, when I used this trick, FMOD would crash when I called System::release(). Should I avoid copying and instead rely on allocating heap memory for long term EventInstance objects?
> 
> This shouldn’t cause a crash - sorry about that! We’ll look into it.

I haven’t been able to reproduce this crash here. Could you email a code sample that causes the crash to [support@fmod.org](mailto:support@fmod.org) so we can look into it?

---

<div class="post-metadata">

**Author:** ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://qa.fmod.com/u/Kai)\
**Post date:** [August 15, 2013, 1:02am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/4 "2013-08-15T01:02:28Z")

</div>

Thanks for the clarifications! The code I had that took advantage of copying EventInstances was mixed in with other game systems so it’s possible that it was something else. When I have time I’ll try to create a simplified version that reproduces the problem.

---

<div class="post-metadata">

**Author:** ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://qa.fmod.com/u/Kai)\
**Post date:** [August 20, 2013, 3:24am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/5 "2013-08-20T03:24:42Z")

</div>

> [@ben](#):
>
> > [@ben](#):
> >
> > > [@](#):
> > >
> > > However, when I used this trick, FMOD would crash when I called System::release(). Should I avoid copying and instead rely on allocating heap memory for long term EventInstance objects?
> > 
> > This shouldn’t cause a crash - sorry about that! We’ll look into it.
> 
> I haven’t been able to reproduce this crash here. Could you email a code sample that causes the crash to [support@fmod.org](mailto:support@fmod.org) so we can look into it?

I discovered that the crash occurs when calling EventDescription::createInstance on an uninitialized block of memory. I am using [Lua userdata](http://www.lua.org/pil/28.1.html) to store my FMOD EventInstances, which doesn’t initialize the memory when it is allocated. I have to use placement new to explicitly construct the EventInstance, at which point I can use it safely.

Here is the relevant code, for anyone else who might run into this issue:

```auto
FmodEventInstanceUserData* udata = static_cast<FmodEventInstanceUserData*>(lua_newuserdata(L, sizeof(FmodEventInstanceUserData)));
new (&udata->instance) FMOD::Studio::EventInstance();
ERRCHECK( eventDescription.createInstance(&udata->instance) );
```

---

<div class="post-metadata">

**Author:** ![geoff](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@geoff](https://qa.fmod.com/u/geoff)\
**Post date:** [August 20, 2013, 7:07am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/6 "2013-08-20T07:07:48Z")

</div>

Good to hear you have solved your problem. Are you exposing the EventInstance API to lua script? If so, you should also try to ensure the script calls the release() function when finished with the sound.

Also, you will want to set up a finalizer in lua that explicitly calls the EventInstance destructor, as explained here:

[http://www.lua.org/manual/5.1/manual.html#2.10.1](http://www.lua.org/manual/5.1/manual.html#2.10.1)

In fact it should call release() and then the destructor. That will catch cases where the script isn’t calling release() and just letting your userdata go free, although since it is tied to garbage collection it may not happen in a particularly timely fashion. That isn’t a big deal but it could mean a few extra memory allocations hang around for longer than they might otherwise.

Let us know if you have any further issues.

---

<div class="post-metadata">

**Author:** ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://qa.fmod.com/u/Kai)\
**Post date:** [August 20, 2013, 11:42am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/7 "2013-08-20T11:42:08Z")

</div>

> [@geoff](#):
>
> Good to hear you have solved your problem. Are you exposing the EventInstance API to lua script? If so, you should also try to ensure the script calls the release() function when finished with the sound.
> 
> Also, you will want to set up a finalizer in lua that explicitly calls the EventInstance destructor, as explained here:
> 
> [http://www.lua.org/manual/5.1/manual.html#2.10.1](http://www.lua.org/manual/5.1/manual.html#2.10.1)
> 
> In fact it should call release() and then the destructor. That will catch cases where the script isn’t calling release() and just letting your userdata go free, although since it is tied to garbage collection it may not happen in a particularly timely fashion. That isn’t a big deal but it could mean a few extra memory allocations hang around for longer than they might otherwise.
> 
> Let us know if you have any further issues.

Hey geoff, thanks for the advice! I am calling release() in the \_\_gc metamethod like you suggest.

However, unless I’m missing something, I don’t think I can call the EventInstance destructor myself. According to fmod\_studio.hpp, the EventInstance inherits its destructor from Handle, which in turn has a protected destructor that I can’t access. I assume C++ generates a default destructor for EventInstance but I can’t seem to access that either.

---

<div class="post-metadata">

**Author:** ![geoff](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@geoff](https://qa.fmod.com/u/geoff)\
**Post date:** [August 21, 2013, 2:13am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/8 "2013-08-21T02:13:59Z")

</div>

You should be able to access the EventInstance destructor as in the following:

```auto
udata->instance.~EventInstance();
```

---

<div class="post-metadata">

**Author:** ![Kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kai](https://qa.fmod.com/u/Kai)\
**Post date:** [August 21, 2013, 2:40am UTC](https://qa.fmod.com/t/eventdescription-and-eventinstance-caching-in-c/11103/9 "2013-08-21T02:40:09Z")

</div>

> [@geoff](#):
>
> You should be able to access the EventInstance destructor as in the following:
> 
> ```auto
> udata->instance.~EventInstance();
> ```

Ah, my problem was that I’m not using the FMOD namespace. Apparently it’s not possible to explicitly call destructors of objects unless you import the namespace.

This DOESNT work:

```auto
instance.~FMOD::Studio::EventInstance();
```

Here is what I needed to do to get it to work:

```auto
{
  using namespace FMOD::Studio;
  instance.~EventInstance();
}
```

Thanks for the help!
