Garbage collection issue

I have a subclass of UGameInstanceSubsystem for managing background music in my game. It keeps a stack of audio components (allocated through UGameplayStatics::SpawnSound2D()) so I can have eg menu music take over from game music temporarily.

UPROPERTY()
TArray<UAudioComponent *> trackStack_{};

My front end UMG class plays some audio via this, plays nicely, and when I hit “Go!” it opens a new level (OpenLevelByName). But when that level loads, the current playing track becomes null in my subsystem, even though I’ve UPROPERTY’d the array. Even though game instance subsystems live outside of levels. Now I can put defensive guards around code to check for nulls, but it seems I’m missing something here. Why was that audio component GC’d?

Are you attaching the audio components? Rootless components can be gc’d

1 Like

No they just live in that array. As they’re in a subsystem there’s nothing to attach them to!

Do I AddToRoot() them?

Then the components need a parent. You can try creating an actor that behaves as an audio manager. Attach the audio components to it.

Ok… won’t the actor die when the new level loads and so I’ll still have the same issue?

You could just use a struct array that holds relevent information regarding the audio components and then reconstruct them on load.

Well turns out there’s a simpler answer :rofl:

There’s a (defaulted to false) flag you can specify when you call SpawnSound2D() “bPersistAcrossLevelTransitions” and setting that to true makes the crashes go away :slight_smile:

2 Likes