# Particle system being garbage collected

**URL:** <https://forums.unrealengine.com/t/particle-system-being-garbage-collected/385483>\
**Category:** FX\
**Tags:** garbage-collector, particle-systems, unreal-engine, question, CPP\
**Created:** [February 19, 2017, 11:55pm UTC](https://forums.unrealengine.com/t/particle-system-being-garbage-collected/385483 "2017-02-19T23:55:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_1c4cdb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1c4cdb3c](https://forums.unrealengine.com/u/anonymous_user_1c4cdb3c)\
**Post date:** [February 19, 2017, 11:55pm UTC](https://forums.unrealengine.com/t/particle-system-being-garbage-collected/385483/1 "2017-02-19T23:55:19Z")

</div>

I want to spawn explosions when an actor is destroyed. Seems easy, I just give each actor a UParticleSystem\* pointer and initialize it in the actor’s constructor with whatever explosion I want for that particular actor. When the actor’s hitpoints reach zero, I call SpawnEmitterAtLocation() and pass in the UParticleSystem\* pointer. The emitter is set to auto-destroy, one loop, etc. Mostly, it appears to work correctly.

Problem is that the particle system sometimes gets garbage collected (I think). I’ll have a screen full of explosions and they’ll all just periodically disappear. My guess is that the garbage collector runs, sees that the actor that owned the pointer has been destroyed, and cleans it because there are no remaining pointers to it.

I tried a really hacky solution of forcing the actor to hang around for a few seconds before destroying it, but that has worse problems. For one, each type of explosion might have a different duration, and I don’t know a way in C++ to get an emitter’s duration. For another, doing it this way means I have to play games with making the actor invisible and turning off its collision, etc. Not clean.

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [February 20, 2017, 1:49pm UTC](https://forums.unrealengine.com/t/particle-system-being-garbage-collected/385483/2 "2017-02-20T13:49:14Z")

</div>

Why not use the GameplayStatics instead of keeping a pointer to the Particle System Component on each actor? You can rather store a UParticleSystem\* (i.e. not Component) reference on your actors and pass that to UGameplayStatics::SpawnEmitterAtLocation(…).

I might be wrong, but I think this uses the emitter pool so you will possibly gain a large performance benefit as well.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_1c4cdb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1c4cdb3c](https://forums.unrealengine.com/u/anonymous_user_1c4cdb3c)\
**Post date:** [February 20, 2017, 5:05pm UTC](https://forums.unrealengine.com/t/particle-system-being-garbage-collected/385483/3 "2017-02-20T17:05:32Z")

</div>

That’s how I do it. Here is the call in my actor’s code:

```
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), m_explosion_ps, GetActorLocation(), FRotator::ZeroRotator);
Destroy();

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_1c4cdb3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1c4cdb3c](https://forums.unrealengine.com/u/anonymous_user_1c4cdb3c)\
**Post date:** [February 20, 2017, 6:21pm UTC](https://forums.unrealengine.com/t/particle-system-being-garbage-collected/385483/4 "2017-02-20T18:21:17Z")

</div>

I just found a forum thread where I asked about this nearly two years ago:

> **[Spawn particle system in Destroyed()](https://forums.unrealengine.com/showthread.php?73548-Spawn-particle-system-in-Destroyed%28%29)**
>
> I’ve been spawning explosion particle systems when something is destroyed in the Destroyed() method. It finally dawned on me that this is why my explosions randomly disappear before finishing. If the owning actor is gone, the particle system can be...

There wasn’t a solid answer there, but another user mentioned having the same/similar problem.

I think that I may have to resort to having an “explosion actor.” Just an empty actor with a particle system component. I believe I can use OnSystemFinished to know when the explosion is done and then Destroy() the actor.

This is a heavy workaround and is going to have a performance cost.
