From 714b19badc4944154b1cc37e0ba684c5677af007 Mon Sep 17 00:00:00 2001 From: durswd Date: Sun, 2 Jun 2024 17:22:02 +0900 Subject: [PATCH] Fix a memory bug --- .../Private/EffekseerEmitterComponent.cpp | 134 +++++++++++------- .../Public/EffekseerEmitterComponent.h | 4 +- 2 files changed, 84 insertions(+), 54 deletions(-) diff --git a/Plugins/Effekseer/Source/Effekseer/Private/EffekseerEmitterComponent.cpp b/Plugins/Effekseer/Source/Effekseer/Private/EffekseerEmitterComponent.cpp index 4e805b6..cd44849 100644 --- a/Plugins/Effekseer/Source/Effekseer/Private/EffekseerEmitterComponent.cpp +++ b/Plugins/Effekseer/Source/Effekseer/Private/EffekseerEmitterComponent.cpp @@ -19,10 +19,10 @@ class FEffekseerEmitterSceneProxy : public FPrimitiveSceneProxy { private: const UEffekseerEmitterComponent* component_ = nullptr; - std::vector handles_; + TArray handles_; public: - FEffekseerEmitterSceneProxy(const UEffekseerEmitterComponent* InComponent, std::vector handles) + FEffekseerEmitterSceneProxy(const UEffekseerEmitterComponent* InComponent, TArray handles) : FPrimitiveSceneProxy(InComponent) { component_ = InComponent; @@ -105,15 +105,15 @@ class FEffekseerEmitterSceneProxy : public FPrimitiveSceneProxy if (updateData->Type == EffekseerEmitterUpdateData_CommandType::Add) { - handles_.push_back(updateData->Handle); + handles_.Add(updateData->Handle); } else if (updateData->Type == EffekseerEmitterUpdateData_CommandType::Remove) { - for (size_t i = 0; i < handles_.size(); i++) + for (int32_t i = 0; i < handles_.Num(); i++) { if (handles_[i].ID == updateData->Handle.ID) { - handles_.erase(handles_.begin() + i); + handles_.RemoveAt(i); break; } } @@ -149,24 +149,19 @@ class FEffekseerEmitterSceneProxy : public FPrimitiveSceneProxy { return component_; } - - std::vector GetHandles() const - { - return handles_; - } }; -void UEffekseerEmitterComponent::ApplyParameters(bool forced) +void UEffekseerEmitterComponent::ApplyParameters(const FEffekseerHandle& handle, bool forced) { if (AllColor != AllColor_ || forced) { - system_->SetEffectAllColor(handle_, AllColor); + system_->SetEffectAllColor(handle, AllColor); AllColor_ = AllColor; } if (Speed != Speed_ || forced) { - system_->SetEffectSpeed(handle_, Speed); + system_->SetEffectSpeed(handle, Speed); Speed_ = Speed; } @@ -175,7 +170,7 @@ void UEffekseerEmitterComponent::ApplyParameters(bool forced) { if (DynamicInput[i] != DynamicInput_[i] || forced) { - system_->SetEffectDynamicInput(handle_, i, DynamicInput[i]); + system_->SetEffectDynamicInput(handle, i, DynamicInput[i]); isDynamicInputChanged = true; } } @@ -284,7 +279,7 @@ FPrimitiveSceneProxy* UEffekseerEmitterComponent::CreateSceneProxy() { if (sceneProxy_ != nullptr) { - auto sp = new FEffekseerEmitterSceneProxy(sceneProxy_->GetInComponent(), sceneProxy_->GetHandles()); + auto sp = new FEffekseerEmitterSceneProxy(this, handles_); sceneProxy_ = sp; } else @@ -382,52 +377,71 @@ void UEffekseerEmitterComponent::TickComponent(float DeltaTime, ELevelTick TickT if (handle.Effect != nullptr) { - handle_ = handle; + handles_.Add(handle); isPlaying = true; shouldActivate = false; - ApplyParameters(true); + ApplyParameters(handle, true); } } if (system_ != nullptr && isPlaying) { - if (system_->Exists(handle_)) - { - auto transform = GetComponentToWorld(); - - system_->SetEffectPosition(handle_, transform.GetTranslation()); - system_->SetEffectRotation(handle_, transform.GetRotation()); - system_->SetEffectScaling(handle_, transform.GetScale3D()); - - ApplyParameters(false); - } - else + for (int i = 0; i < handles_.Num();) { + auto handle = handles_[i]; + if (system_->Exists(handle)) { - auto cmd = new EffekseerEmitterUpdateData_Command(); - cmd->Type = EffekseerEmitterUpdateData_CommandType::Remove; - cmd->Handle = handle_; - sceneProxy_->UpdateData(cmd); - handle_ = FEffekseerHandle(); - } + auto transform = GetComponentToWorld(); - if (IsLooping) - { - AllColor_ = FColor(255, 255, 255, 255); - Speed_ = 1.0f; - handle_ = PlayInternal(); + system_->SetEffectPosition(handle, transform.GetTranslation()); + system_->SetEffectRotation(handle, transform.GetRotation()); + system_->SetEffectScaling(handle, transform.GetScale3D()); + + ApplyParameters(handle, false); + i++; } else { - isPlaying = false; + { + auto cmd = new EffekseerEmitterUpdateData_Command(); + cmd->Type = EffekseerEmitterUpdateData_CommandType::Remove; + cmd->Handle = handle; + sceneProxy_->UpdateData(cmd); + } + + if (IsLooping) + { + AllColor_ = FColor(255, 255, 255, 255); + Speed_ = 1.0f; + auto new_handle = PlayInternal(); - if (bAutoDestroy) + if (new_handle.Effect != nullptr) + { + handles_[i] = new_handle; + ApplyParameters(new_handle, true); + i++; + } + else + { + handles_.RemoveAt(i); + } + } + else { - DestroyComponent(); - return; + handles_.RemoveAt(i); } } } + + if (handles_.Num() == 0) + { + isPlaying = false; + + if (bAutoDestroy) + { + DestroyComponent(); + } + } } Super::TickComponent(DeltaTime, TickType, ThisTickFunction); @@ -466,15 +480,20 @@ void UEffekseerEmitterComponent::Stop() { if (system_ != nullptr) { - system_->Stop(handle_); - + for (auto handle : handles_) { - auto cmd = new EffekseerEmitterUpdateData_Command(); - cmd->Type = EffekseerEmitterUpdateData_CommandType::Remove; - cmd->Handle = handle_; - sceneProxy_->UpdateData(cmd); + system_->Stop(handle); + + { + auto cmd = new EffekseerEmitterUpdateData_Command(); + cmd->Type = EffekseerEmitterUpdateData_CommandType::Remove; + cmd->Handle = handle; + sceneProxy_->UpdateData(cmd); + } } + handles_.Empty(); + isPlaying = false; } } @@ -483,7 +502,10 @@ void UEffekseerEmitterComponent::StopRoot() { if (system_ != nullptr) { - system_->StopRoot(handle_); + for (auto handle : handles_) + { + system_->StopRoot(handle); + } } } @@ -491,7 +513,12 @@ bool UEffekseerEmitterComponent::Exists() const { if (system_ != nullptr) { - return system_->Exists(handle_); + bool exists = false; + for (auto handle : handles_) + { + exists |= system_->Exists(handle); + } + return exists; } return false; } @@ -500,7 +527,10 @@ void UEffekseerEmitterComponent::SendTrigger(int index) { if (system_ != nullptr) { - return system_->SendTrigger(handle_, index); + for (auto handle : handles_) + { + system_->SendTrigger(handle, index); + } } } diff --git a/Plugins/Effekseer/Source/Effekseer/Public/EffekseerEmitterComponent.h b/Plugins/Effekseer/Source/Effekseer/Public/EffekseerEmitterComponent.h index db61673..aaedcfd 100644 --- a/Plugins/Effekseer/Source/Effekseer/Public/EffekseerEmitterComponent.h +++ b/Plugins/Effekseer/Source/Effekseer/Public/EffekseerEmitterComponent.h @@ -20,7 +20,7 @@ class EFFEKSEER_API UEffekseerEmitterComponent : public UPrimitiveComponent FEffekseerEmitterSceneProxy* sceneProxy_ = nullptr; bool shouldActivate = false; bool isPlaying = false; - FEffekseerHandle handle_; + TArray handles_; FColor AllColor_ = FColor(255, 255, 255, 255); float Speed_ = 1.0f; @@ -35,7 +35,7 @@ class EFFEKSEER_API UEffekseerEmitterComponent : public UPrimitiveComponent UPROPERTY(Transient) TArray materials_; - void ApplyParameters(bool forced); + void ApplyParameters(const FEffekseerHandle& handle, bool forced); FEffekseerHandle PlayInternal();