Why the runtime error occurs in AddDynamic function of multicast dynamic delegate?

Hello. I have created my hit box component based on USceneComponent.
Code of this component:

header file:

#include "Components/ActorComponent.h"
#include "HitBoxComponent.generated.h"

/** Delegate for handling OnHit event*/
DECLARE_DELEGATE_FourParams(FHitBoxHitSignature, class AActor*, class UPrimitiveComponent*, FVector, const FHitResult&);

/** Delegate for handling OnBeginOverlap event*/
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams(FHitBoxHitEvent, class AActor*, OtherActor, class UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit);

/** Type of hitbox*/
UENUM(BlueprintType)
enum class HitBoxType : uint8
{
	/** Head hitbox */
	HitBoxType_HEAD UMETA(DisplayName="Head hitbox"),
	
	/** Stomach hitbox */
	HitBoxType_STOMACH UMETA(DisplayName="Stomach hitbox"),
	
	/** Left arm hitbox */
	HitBoxType_LEFT_ARM UMETA(DisplayName="Left arm hitbox"),
	
	/** Right arm hitbox */
	HitBoxType_RIGHT_ARM UMETA(DisplayName="Right arm hitbox"),
	
	/** Left leg hitbox */
	HitBoxType_LEFT_LEG UMETA(DisplayName="Left leg hitbox"),
	
	/** Right leg hitbox */
	HitBoxType_RIGHT_LEG UMETA(DisplayName="Right leg hitbox")
};

UCLASS(Blueprintable, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class FIGHINGGAME_API UHitBoxComponent : public USceneComponent
{
	GENERATED_UCLASS_BODY()

public:

	/** Shape of hitbox*/
	UPROPERTY(BlueprintReadOnly, EditAnywhere)
	UPrimitiveComponent *HitBoxShape;

	/** Health of hitbox */
	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	float HitBoxHealth;

	/** Type of hitbox */
	UPROPERTY(BlueprintReadOnly, EditAnywhere)
	HitBoxType TypeOfHitBox;

	/** Name of hitbox*/
	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	FName HitBoxName;

	UPROPERTY(BlueprintAssignable)
	FHitBoxHitEvent HitBoxHitEvent;

public:	
	// Sets default values for this component's properties
	UHitBoxComponent();

	// Called when the game starts
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

	UFUNCTION()
	void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};

cpp file:

#include "FighingGame.h"
#include "HitBoxComponent.h"


// Sets default values for this component's properties
UHitBoxComponent::UHitBoxComponent(const FObjectInitializer &Initializer)
	: Super(Initializer)
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	bWantsBeginPlay = true;
	PrimaryComponentTick.bCanEverTick = true;

	HitBoxShape = Initializer.CreateAbstractDefaultSubobject<UPrimitiveComponent>(this, TEXT("CollisionComponent"));
	HitBoxShape->OnComponentHit.AddDynamic(this, &UHitBoxComponent::OnHit);
}

void UHitBoxComponent::OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	GEngine->AddOnScreenDebugMessage(0, 1.f, FColor::Red, TEXT("UHitBoxComponent::OnHit"));
	HitBoxHitEvent.Broadcast(OtherActor, OtherComp, NormalImpulse, Hit);
}

// Called when the game starts
void UHitBoxComponent::BeginPlay()
{
	Super::BeginPlay();
}


// Called every frame
void UHitBoxComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
	Super::TickComponent( DeltaTime, TickType, ThisTickFunction );

	// ...
}

This code is successfully compiled. But when I start the project, the next exception occurs:

This exception occurs in file Array.h in code:

/**
	 * Remove all instances that match the predicate
	 *
	 * @param Predicate Predicate class instance
	 */
	template <class PREDICATE_CLASS>
	void RemoveAllSwap(const PREDICATE_CLASS& Predicate, bool bAllowShrinking = true)
	{
		for (int32 ItemIndex = 0; ItemIndex < Num();) //< Here exception occurs
		{
			if (Predicate((*this)[ItemIndex]))
			{
				RemoveAtSwap(ItemIndex, 1, bAllowShrinking);
			}
			else
			{
				++ItemIndex;
			}
		}
	}

Why this exception occurs?

I am having this same issue

Could you please try replacing CreateAbstractDefaultSubobject with CreateDefaltSubobject. Noting that you have an access violation, meaning you dereference a null pointer, my guess is that CreateAbstractDefaultSubobject returns NULL. CreateDefaltSubobject should do just fine.

I still get this in 4.18.2 with a USphereComponent as the collision sphere that I bind the OnComponentBeginOverlap to. I’ve found that deleting the Blueprint that I’ve made from the .cpp file and remaking the Blueprint fixes it. Seems like the BP gets corrupted somehow and no amount of recompiling fixes it.

I’ve also noticed that when this happens, the component that it’s called from has an empty Details panel in the Blueprint.