# Reference Monobehaviours from PCMReadCallback?

**URL:** <https://qa.fmod.com/t/reference-monobehaviours-from-pcmreadcallback/12267>\
**Category:** Unity\
**Created:** [January 24, 2016, 5:45am UTC](https://qa.fmod.com/t/reference-monobehaviours-from-pcmreadcallback/12267 "2016-01-24T05:45:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![haydenjameslee](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@haydenjameslee](https://qa.fmod.com/u/haydenjameslee)\
**Post date:** [January 24, 2016, 5:45am UTC](https://qa.fmod.com/t/reference-monobehaviours-from-pcmreadcallback/12267/1 "2016-01-24T05:45:53Z")

</div>

I have created a custom audio generator and want to play it through FMOD in Unity on Windows. However whenever I access a Monobehaviour from PCMReadCallback Unity freezes. So far I’ve worked out that the callback is run on a different thread (I’m assuming a thread spawned by FMOD for audio processing) and I’m guessing due to Unity’s lack of multithreading support this is causing issues. How can I get around this and get data from another object into the FMOD callback from an external generator?

[Here’s an example of code snippet that I’d like to work.](https://gist.github.com/haydenjameslee/3e20983cf5530e878a98)

---

<div class="post-metadata">

**Author:** ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)\
**Post date:** [January 26, 2016, 11:21pm UTC](https://qa.fmod.com/t/reference-monobehaviours-from-pcmreadcallback/12267/2 "2016-01-26T23:21:05Z")

</div>

Although I’m not sure it will fix all your issues I’ve spotted a common flaw in your code.

```
exinfo.pcmreadcallback = pcmreadcallback;

```

is short hand for

```
var cb = new SOUND_PCMREADCALLBACK(pcmreadcallback);
exinfo.pcmreadcallback = cb;

```

and now you’ve passed a reference to FMOD of a managed object that is only in the local scope and will garbage collected some time after your FMODStreamPlayer.CreateStream method has finished.

You need initialise a SOUND\_PCMREADCALLBACK (and also a SOUND\_PCMSETPOSCALLBACK) class member variable that will stay around while FMOD is still issuing the callback.

---

<div class="post-metadata">

**Author:** ![haydenjameslee](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@haydenjameslee](https://qa.fmod.com/u/haydenjameslee)\
**Post date:** [January 27, 2016, 11:40pm UTC](https://qa.fmod.com/t/reference-monobehaviours-from-pcmreadcallback/12267/3 "2016-01-27T23:40:22Z")

</div>

> [@](#):
>
> Although I’m not sure it will fix all your issues I’ve spotted a common flaw in your code.
> 
> ```
> exinfo.pcmreadcallback = pcmreadcallback;
> 
> ```
> 
> is short hand for
> 
> ```
> var cb = new SOUND_PCMREADCALLBACK(pcmreadcallback);
> exinfo.pcmreadcallback = cb;
> 
> ```
> 
> and now you’ve passed a reference to FMOD of a managed object that is only in the local scope and will garbage collected some time after your FMODStreamPlayer.CreateStream method has finished.
> 
> You need initialise a SOUND\_PCMREADCALLBACK (and also a SOUND\_PCMSETPOSCALLBACK) class member variable that will stay around while FMOD is still issuing the callback.

This seemed to be causing the issue. Its working now and I can reference other components from the callback. There is still an issue with Unity sometimes crashing when I stop the editor however.

---

<div class="post-metadata">

**Author:** ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)\
**Post date:** [January 27, 2016, 11:42pm UTC](https://qa.fmod.com/t/reference-monobehaviours-from-pcmreadcallback/12267/4 "2016-01-27T23:42:09Z")

</div>

Be very conservative in what managed objects you access. Try not to allocate any managed objects.
