Announcement

Collapse
No announcement yet.

"new" without "delete" in ARPG

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    "new" without "delete" in ARPG

    Hi all!

    One of the most important things we're taught when learning C++ is that for every "new" there must be a "delete". Now, programming with Unreal usually doesn't require manual memory management and I haven't had the need to write a single "new" in years working with UE4. But I'm currently getting familiar with the Gameplay Ability Plugin and I'm using the Action RPG sample as reference. In RPGAbilityTypes.cpp there's this function:

    Code:
    void FRPGGameplayEffectContainerSpec::AddTargets(const TArray<FHitResult>& HitResults, const TArray<AActor*>& TargetActors)
    {
        for (const FHitResult& HitResult : HitResults)
        {
            FGameplayAbilityTargetData_SingleTargetHit* NewData = new FGameplayAbilityTargetData_SingleTargetHit(HitResult);
            TargetData.Add(NewData);
        }
    
        if (TargetActors.Num() > 0)
        {
            FGameplayAbilityTargetData_ActorArray* NewData = new FGameplayAbilityTargetData_ActorArray();
            NewData->TargetActorArray.Append(TargetActors);
            TargetData.Add(NewData);
        }
    }
    I tried to follow the object created in a line such as this,
    Code:
    FGameplayAbilityTargetData_SingleTargetHit* NewData = new FGameplayAbilityTargetData_SingleTargetHit(HitResult);
    but I find it hard to grep for that, so now I'm left wondering if this is actually the proper way to do this particular task. It might very well be that the data gets cleaned up later, somewhere deep within the bowels of the engine, but like I said, I haven't really found that place.

    Can anyone confirm that the above code does not leak memory, maybe even point me to where in the UE codebase this is taken care of?

    Thanks in advance!

    #2
    That will definitely leak unless you call delete, since USTRUCTS aren't UObjects and not subject to the same GC rules.

    Likely TargetData is calling delete on the members when it cleans up (or the sample leaks and no one cared to fix it).
    Able Ability System - A high performance, robust ability system for UE4. Now Available!

    Comment

    Working...
    X