# Manage INVALID HANDLE channel correctly

**URL:** https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277
**Category:** FMOD Engine
**Tags:** cpp
**Created:** [June 2, 2023, 1:15pm UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277 "2023-06-02T13:15:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![richarrr](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@richarrr](https://qa.fmod.com/u/richarrr)
#### Post date: [June 2, 2023, 1:15pm UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/1 "2023-06-02T13:15:46Z")

</div>

Hi,

Here is my scenario:  
First I call playSound, this creates a ‘Channel\* chan’ object.  
Let’s say after a few seconds, I want to modify something on ‘chan’ ( modify frequency, fade out, stop … ).  
My concern is that if the sound has already finished playing it seems FMOD will automatically delete the ‘chan’ object. And all my calls on ‘chan’ will return error with ‘FMOD\_ERR\_INVALID\_HANDLE’.

My questions:  
When I use my invalid ‘chan’ handle. Does FMOD means:  
“I know this is a channel handle that has stopped, so I won’t modify it”  
or  
“I don’t know what ‘chan’ is, it musn’t be used in the API anymore”.  
in other words, is it illegal to use an invalid ‘chan’ ( like using a deleted object in C++ ) ?

If it’s illegal, what’s the easiest way to track if my ‘chan’ is still valid, I need a way to set it to ‘nullptr’ when FMOD deletes it internally.

Thanks

* * *

---

<div class="post-metadata">

### Author: ![andy55](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/andy55/32/5016_2.png) [@andy55](https://qa.fmod.com/u/andy55)
#### Post date: [June 5, 2023, 2:48pm UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/2 "2023-06-05T14:48:06Z")

</div>

Hi,

I’m relatively new to FMOD too and found this confusing as well.

I mention [here](https://qa.fmod.com/t/sound-callbacks-in-ios-background-mode/20256) about using [ChannelControl::setCallBack()](https://www.fmod.com/docs/2.02/api/core-api-channelcontrol.html#channelcontrol_setcallback) to get a callback when the sound ends so maybe that works for you. As I mentioned, I am still trying to figure out why it is not firing when iOS is in background mode.

---

<div class="post-metadata">

### Author: ![richarrr](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@richarrr](https://qa.fmod.com/u/richarrr)
#### Post date: [June 5, 2023, 3:57pm UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/3 "2023-06-05T15:57:44Z")

</div>

Thanks for the reply -  
yes I had the idea of adding callback for each sounds I wanted to track. but that feel a bit over-engineered. I hope there is something simpler

---

<div class="post-metadata">

### Author: ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)
#### Post date: [June 6, 2023, 4:16am UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/4 "2023-06-06T04:16:22Z")

</div>

Hi,

I will link to our white papers that outline our handle system: [FMOD API | White Papers - Handle System](https://www.fmod.com/docs/2.02/api/white-papers-handle-system.html). In summary, it is closest to your first answer but not exactly. As it says in our docs “If a Channel is stopped with ChannelControl::stopor ends naturally, the Channel handle will become invalid and return `FMOD_ERR_INVALID_HANDLE`” so it will be safe to still interact with it however, it will just return an invalid handle. A way to check if a channel is still valid is using the channel in an FMOD function and checking the result for `FMOD_ERR_INVALID_HANDLE`.

Hope this clears it up a bit.

---

<div class="post-metadata">

### Author: ![richarrr](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@richarrr](https://qa.fmod.com/u/richarrr)
#### Post date: [June 6, 2023, 7:30am UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/5 "2023-06-06T07:30:57Z")

</div>

Thanks for the detail.  
So my understanding is that on my app side, I don’t need to track the life time of all my channel handles. There is no risk of undefined behavior when using an invalid one.  
That makes my developer life easier.

---

<div class="post-metadata">

### Author: ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)
#### Post date: [June 7, 2023, 6:13am UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/6 "2023-06-07T06:13:45Z")

</div>

Correct, if a stopped channel is used in an FMOD function it will return the `FMOD_RESULT` `FMOD_ERR_INVALID_HANDLE`.

---

<div class="post-metadata">

### Author: ![richarrr](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@richarrr](https://qa.fmod.com/u/richarrr)
#### Post date: [June 7, 2023, 7:57am UTC](https://qa.fmod.com/t/manage-invalid-handle-channel-correctly/20277/7 "2023-06-07T07:57:52Z")

</div>

Thanks for the confirmation
