# Preventing AudioComponent from being destroyed

**URL:** <https://forums.unrealengine.com/t/preventing-audiocomponent-from-being-destroyed/426200>\
**Category:** Audio\
**Tags:** nullptr, unreal-engine, UE4, question, CPP\
**Created:** [May 29, 2018, 2:49pm UTC](https://forums.unrealengine.com/t/preventing-audiocomponent-from-being-destroyed/426200 "2018-05-29T14:49:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avengar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Avengar](https://forums.unrealengine.com/u/Avengar)\
**Post date:** [May 29, 2018, 2:49pm UTC](https://forums.unrealengine.com/t/preventing-audiocomponent-from-being-destroyed/426200/1 "2018-05-29T14:49:29Z")

</div>

Hello there!

Going straight to the problem. I’m trying to create UAudioComponent that can be paused and hold into memory until I decide to delete it. Now i have problem with it becouse after some time on level my sound just disappears.

This is how I’m creating it right now:

```
UAudioComponent* UAudioController::Sound2D(UObject* WorldContextObject, class USoundBase* Sound, float VolumeMultiplier, float PitchMultiplier, float StartTime)
{
	if (!Sound)
	{
		return nullptr;
	}

	UAudioComponent* AudioComponent = UGameplayStatics::CreateSound2D(WorldContextObject, Sound, VolumeMultiplier, PitchMultiplier, StartTime);
	PlaybackPositionMap.Add(AudioComponent, 0.f);
	BindDelegates(AudioComponent);
	AudioComponent->Play(StartTime);
	return AudioComponent;
}

```

PlaybackPositionMap is just TMap that holds reference to UAudioComponent and float that is time played from that sound.

BindDelegates function looks like this:

```
void UAudioController::BindDelegates(UAudioComponent* AudioComponent)
{
	if (AudioComponent->IsValidLowLevel())
	{
		AudioComponent->OnAudioPlaybackPercent.AddDynamic(this, &UAudioController::SavePlaybackPosition);
		AudioComponent->OnAudioFinished.AddDynamic(this, &UAudioController::SoundFinished);
	}
}

```

I do not think you need it but just to be clear with it. I tried saving reference to UAudioComponent in arrays maps static components and disallowing it’s destruction but it always dissapears. Any way I can change that?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_7b20387d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_7b20387d](https://forums.unrealengine.com/u/anonymous_user_7b20387d)\
**Post date:** [November 22, 2019, 2:11am UTC](https://forums.unrealengine.com/t/preventing-audiocomponent-from-being-destroyed/426200/2 "2019-11-22T02:11:00Z")

</div>

Most likely your AudioComponent is getting picked up and destroyed by the garbage collector. I found two workarounds for this:

-Call “AddToRoot()” on your audio component, which will prevent it from getting garbage collected. Call RemoveFromRoot() when you are done with it.

-Store your reference to the component inside a UPROPERTY. If you have a UPROPERTY pointing to your AudioComponent, that should keep it from getting garbage collected.
