ProcessMulticastDelegate throws exception: read access violation

Hi,

I’m having a read access exception thrown by the ProcessMulticastDelegate. (See image attached to see where)

My Source code:

#pragma once

#include "Components/ActorComponent.h"
#include "Components/LaserComponent.h"
#include "LaserReceiverComponent.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRemoveLaserDelegate, ULaserComponent*, LaserComponent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnAddLaserDelegate, ULaserComponent*, LaserComponent);

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class MYPROJECT_API ULaserReceiverComponent : public UActorComponent
{
	GENERATED_BODY()

public:

	UPROPERTY(Category= MyCategory, BlueprintAssignable)
	FOnRemoveLaserDelegate OnRemoveLaser;

	UPROPERTY(Category = MyCategory, BlueprintAssignable)
	FOnAddLaserDelegate OnAddLaser;

	ULaserReceiverComponent();

	virtual void BeginPlay() override;

	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	void AddLaser(ULaserComponent* Laser);

	void RemoveLaser(ULaserComponent* Laser);
};

CPP

#include "MyProject.h"
#include "LaserReceiverComponent.h"



ULaserReceiverComponent::ULaserReceiverComponent() :Super()
{
	PrimaryComponentTick.bCanEverTick = true;
	bWantsBeginPlay = true;
}

void ULaserReceiverComponent::BeginPlay()
{
	Super::BeginPlay();
}

void ULaserReceiverComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}

void ULaserReceiverComponent::AddLaser(ULaserComponent* Laser)
{
	OnAddLaser.Broadcast(Laser);
}

void ULaserReceiverComponent::RemoveLaser(ULaserComponent* Laser)
{
	OnRemoveLaser.Broadcast(Laser);
}

CALLER CPP

void ULaserComponent::SetNewReceiver(ULaserReceiverComponent* NewReceiver)
{
	if (NewReceiver == Receiver)
	{
		return;
	}

	if (Receiver)
	{
		Receiver->RemoveLaser(this);
	}
	if (NewReceiver)
	{
		Receiver->AddLaser(this);
	}
	
	Receiver = NewReceiver;
}

Hi,

You’ve called AddLaser on Receiver, yet it was NewReceiver you tested for null.

Steve

Hi Steve,

Thanks for your answer. I feel dumb, now :slight_smile:

I still don’t understand why it throws the exception in ProcessMulticastDelegate and ULaserReceiverComponent::AddLaser is able to execute having a null Object.

My main expertise was Java before I started learning UE4.

Thanks very much for you answer.

Cheers,
J

C++ isn’t like Java. Dereferencing a null pointer doesn’t throw an exception, it causes undefined behaviour. This can cause it to anything at all, including throw a hardware exception (which it does in your case, eventually, halfway through that function) or even have no visible effects at all.

http://en.cppreference.com/w/cpp/language/ub

“Examples of undefined behavior are … null pointer dereference… and the compiled program is not required to do anything meaningful.”

Hope this helps,

Steve

Thanks Steve,

It did ring the bell once you explained it. Very useful link. It’s been 15 years since I studied C++ at uni, and I have a Java mindset that I need to re-educate.

Thanks again.