EXCEPTION_ACCESS_VIOLATION setting variable in object reference

Hi

I’m getting a “Unhandled Exception: EXCEPTION_ACCESS_VIOLATION writing address 0x00000000000004a8” error when i’m trying to set a UTexture from my object reference.

My class is a subclass of UCompositingElementOutput and i’m trying to get get the UTexture from the composure output to my pawns UTexture variable.
I’m setting this class as the output script for the composure output section.
The end goal is to get the render output from the compositing composure.

header file

UCLASS()
class UEVIRTUALPRODUCTION_API UNcPointerCompositingOutput : public UCompositingElementOutput
{
	GENERATED_BODY()

public:

	virtual void RelayOutput_Implementation(UTexture* RenderResult, UComposurePostProcessingPassProxy* PostProcessProxy) override;
	
	UPROPERTY(EditAnywhere)
	UTexture* ComposureRender;

	UPROPERTY(EditAnywhere)
	TLazyObjectPtr<AVirtualProductionPawn> TargetActor; //select pawn from outliner

	UTexture* ComposurePointerOutputRender;

};

cpp file

void UNcPointerCompositingOutput::RelayOutput_Implementation(UTexture* RenderResult, UComposurePostProcessingPassProxy* PostProcessProxy)
{

	ComposureRender = RenderResult;
	UE_LOG(LogTemp, Display, TEXT("inside the viewport function"));

	UTexture* OutputImage = RenderResult;

	if (RenderResult || TargetActor)
	{
		UE_LOG(LogTemp, Display, TEXT("inside settign fucntion now woop woop"));
		UTexture2D* AsTex2D = Cast<UTexture2D>(RenderResult);


		TargetActor->ComposureOutputRender = AsTex2D; //set UTexture variable pawn in object reference
	}
	else
	{
		UE_LOG(LogTemp, Display, TEXT("nullptr here"));
	}
}

Thanks in advance!

Perhaps your cast is failing? It’s better to check if it works with an if statement


if(UTexture2D* AsTex2D = Cast<UTexture2D>(RenderResult))
{
TargetActor->ComposureOutputRender = AsTex2D; 
}

Also check if your lazy pointer is valid

if(TargetActor->IsValid()){
// do things to target actor
}
2 Likes

I get the same error even if i was just sending it through as a UTexture instead of casting it.

But your second point is food for thought, as I get errors saying.
“no suitable conversion function from “TLazyObjectPtr” to “const UObject *” exists”.

I’ll try getting the object from the outliner another way and hope it works. I’ve not got any experience with TObjectPtr, but could it potentially be the issue here?

I usually resort to TSoftObjectPtr. Haven’t used lazy ones.
With softs I can check them and load them if needed.

1 Like

Wow Thanks a bunch 3dRaven! Spent 2-3 hours figuring out where i was going wrong. I’m not all that fluent with C++.
I switched my TLazyObjectPtr to a TSoftObjectPtr as it looks like it’s a bit more robust and functionality rich. I’ve been looking online to get more familiar with TSoftObjectPtr, i wanted to verify that i could use it just like a raw pointer? ie shouldn’t have issues setting variables in the object it’s referencing and it would be as if i’m using a raw pointer.

For future reference for anyone getting a similar issue (please correct me if i’m wrong):
So the jist of is is that the TLazyObjectPtr was passing the if check to verify it’s not a nullptr. So it would progress when a raw pointer would fail the check at this point because the target actor hasn’t technically loaded yet so the pointer that TLazyObjectPtr was getting was null. But TLazyObjectPtr is not null.

The TLazyObjectPtr couldn’t go in a IsValid() function as it cant go from the TLazyObjectPtr to the raw pointer. I had to use TargetActor.Get() to get the pointer to then pass into the IsValid().
But it looks like the TSoftObjectPtr has the main functionality i want which is to select an actor from the viewport/outliner and get it’s reference. It has the function “TargetActor->IsValid()” so i don’t need to use Get() to then feed into a IsValid() function.

Because my class is a subclass of UCompositingElementOutput, it looks like composure is loading and running before my actor is created in the level.

Also if you need to make sure the pointer is valid in many placed at the same time you could also go the route of TSharedPtr, which will only go invalid once all of it’s instances go out of scope (it’s count can be thread safe)

Just to clarify you’re saying if there is more than 1 object with a pointer to the same object?
Trying to understand when you say that they will go invalid once all of its instances are out of scope. Are you saying it would fail an if statement just like a raw pointer would if it’s null?

You can create an shared pointer reference that holds a pointer to an object. You can then create many shared pointers to it and pass it around your project.

It will only be invalidated once all pointers that point to the original object go out of scope.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.