UE4 found a useful dynamic texture class, but how do I use this with a material without the crash.

This is the header of the class

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "RHI.h"
#include "DynamicTexture.generated.h"

/**
 * 
 */
UCLASS()
class GAME1_API UDynamicTexture : public UObject
{
	GENERATED_BODY()

public:
	// Initializes the dynamic texture with given dimensions
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void Initialize(int32 InWidth, int32 InHeight, FLinearColor InClearColor, TextureFilter FilterMethod = TextureFilter::TF_Nearest);

	// Sets a specified pixel to a color
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void SetPixel(int32 X, int32 Y, FLinearColor Color);

	// Fills the texture with a given color
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void Fill(FLinearColor Color);

	// Fills a rectangle area of the texture with a given color
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void FillRect(int32 X, int32 Y, int32 Width, int32 Height, FLinearColor Color);

	// Draws a line between two points
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void DrawLine(int32 X1, int32 Y1, int32 X2, int32 Y2, FLinearColor Color);

	// Clears the canvas (same as filling with the clear color)
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void Clear();

	// Returns the UTexture resource which is used as a canvas
	UFUNCTION(BlueprintPure, Category = "Dynamic Texture")
		UTexture2D* GetTextureResource();

	// Needs to be called at the end of each drawing operation to update the texture
	// You can also call this at the end of multiple drawing operations, so the UTexture
	// does not get updated more than needed.
	UFUNCTION(BlueprintCallable, Category = "Dynamic Texture")
		void UpdateTexture();

	// Returns the width of this texture
	UFUNCTION(BlueprintPure, Category = "Dynamic Texture")
		int32 GetWidth();

	// Returns the height of this texture
	UFUNCTION(BlueprintPure, Category = "Dynamic Texture")
		int32 GetHeight();

private:
	// Internal function to set a pixel in the image
	void SetPixelInternal(uint8*& Ptr, uint8 Red, uint8 Green, uint8 Blue, uint8 Alpha);

	// Internal function to return the pointer pointing to the specified pixel
	uint8* GetPointerToPixel(int32 X, int32 Y);

private:
	// Reference to the UTexture2D* were drawing to
	UPROPERTY()
		UTexture2D* Texture;

	// The Dimensions of the image we're drawing
	int32 TextureWidth;
	int32 TextureHeight;

	// The clear color of the canvas
	FLinearColor ClearColor;

	// Unique pointer to the raw pixel data of the texture
	TUniquePtr<uint8[]> PixelBuffer;

	// Unique pointer to the proxy used to update the texture region (not really using regions,
	// it's one "big" region)
	TUniquePtr<FUpdateTextureRegion2D> UpdateTextureRegionProxy;
};

source

// Fill out your copyright notice in the Description page of Project Settings.


#include "DynamicTexture.h"

// UTextures have a BPP of 4 (Red, Green, Blue, Alpha)
#define DYNAMIC_TEXTURE_BYTES_PER_PIXEL 4

void UDynamicTexture::Initialize(int32 InWidth, int32 InHeight, FLinearColor InClearColor, TextureFilter FilterMethod/* = TextureFilter::TF_Nearest*/)
{
	// Store the parameters
	TextureWidth = InWidth;
	TextureHeight = InHeight;
	ClearColor = InClearColor;

	// Create the UTexture2D to render to
	Texture = UTexture2D::CreateTransient(TextureWidth, TextureHeight);
	Texture->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
	Texture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap; // The VectorDisplacementMap is a raw RGBA8 format
	Texture->SRGB = 1;
	Texture->Filter = FilterMethod;
	Texture->UpdateResource();

	// Create the proxy object for updating the texture region
	UpdateTextureRegionProxy = MakeUnique<FUpdateTextureRegion2D>(0, 0, 0, 0, TextureWidth, TextureHeight);

	// Size of the image pixel buffer
	SIZE_T BufferSize = TextureWidth * TextureHeight * DYNAMIC_TEXTURE_BYTES_PER_PIXEL;
	PixelBuffer = MakeUnique<uint8[]>(BufferSize);

	// Initially clear the texture
	Clear();
}

void UDynamicTexture::SetPixel(int32 X, int32 Y, FLinearColor Color)
{
	// Get the pointer of the specified pixel
	uint8* Ptr = GetPointerToPixel(X, Y);

	// Set the pixel (note that linear color uses floats between 0..1, but a uint8 ranges from 0..255)
	SetPixelInternal(Ptr, Color.R * 255, Color.G * 255, Color.B * 255, Color.A * 255);
}

void UDynamicTexture::Fill(FLinearColor Color)
{
	// Get the base pointer of the pixel buffer
	uint8* Ptr = PixelBuffer.Get();

	// Iterate over all pixels
	for (int i = 0; i < TextureWidth * TextureHeight; ++i)
	{
		// Set the pixel
		SetPixelInternal(Ptr, Color.R * 255, Color.G * 255, Color.B * 255, Color.A * 255);

		// Advance to the next pixel
		Ptr += DYNAMIC_TEXTURE_BYTES_PER_PIXEL;
	}
}

void UDynamicTexture::FillRect(int32 X, int32 Y, int32 Width, int32 Height, FLinearColor Color)
{
	// Will hold the current pixel
	uint8* Ptr = NULL;

	// Loop over the Y and X region
	for (int y = Y; y < Y + Height; y++)
	{
		for (int x = X; x < X + Width; x++)
		{
			// Get the current pixel pointer
			Ptr = GetPointerToPixel(x, y);

			// Set the pixel
			SetPixelInternal(Ptr, Color.R * 255, Color.G * 255, Color.B * 255, Color.A * 255);
		}
	}
}

void UDynamicTexture::DrawLine(int32 X1, int32 Y1, int32 X2, int32 Y2, FLinearColor Color)
{
	// Bresenham's line algorithm taken from here: http://members.chello.at/~easyfilter/bresenham.html
	int X = X1;
	int Y = Y1;

	int dx = abs(X2 - X1), sx = X1 < X2 ? 1 : -1;
	int dy = -abs(Y2 - Y1), sy = Y1 < Y2 ? 1 : -1;
	int err = dx + dy, e2; // error value e_xy

	for (;;)
	{
		SetPixel(X, Y, Color);
		if (X == X2 && Y == Y2) break;
		e2 = 2 * err;
		if (e2 >= dy) { err += dy; X += sx; } // e_xy+e_x > 0
		if (e2 <= dx) { err += dx; Y += sy; } // e_xy+e_y < 0
	}
}

void UDynamicTexture::Clear()
{
	// Fill with the clear color
	Fill(ClearColor);
}

UTexture2D* UDynamicTexture::GetTextureResource()
{
	return Texture;
}

void UDynamicTexture::SetPixelInternal(uint8*& Ptr, uint8 Red, uint8 Green, uint8 Blue, uint8 Alpha)
{
	// Pixels are stored in BGRA format
	*Ptr = Blue;
	*(Ptr + 1) = Green;
	*(Ptr + 2) = Red;
	*(Ptr + 3) = Alpha;
}

uint8* UDynamicTexture::GetPointerToPixel(int32 X, int32 Y)
{
	// The calculation of the pointer address of a given pixel is
	// base + ((x + (y * width)) * bpp)
	return (PixelBuffer.Get() + ((X + (Y * TextureWidth)) * DYNAMIC_TEXTURE_BYTES_PER_PIXEL));
}

void UDynamicTexture::UpdateTexture()
{
	// Make sure the proxy and the texture is valid
	if (UpdateTextureRegionProxy.IsValid() && Texture)
	{
		// Update the texture's regions
		Texture->UpdateTextureRegions(
			0,											// Mip index
			1,											// Number of regions
			UpdateTextureRegionProxy.Get(),				// Region proxy
			TextureWidth * DYNAMIC_TEXTURE_BYTES_PER_PIXEL,	// Source data pitch
			DYNAMIC_TEXTURE_BYTES_PER_PIXEL,			// Bytes per pixel of source data
			PixelBuffer.Get()							// Buffer of pixels to set
		);
	}
}

int32 UDynamicTexture::GetWidth()
{
	return TextureWidth;
}

int32 UDynamicTexture::GetHeight()
{
	return TextureHeight;
}

This is the header of my code

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "DynamicTexture.h"
#include "ComputerActor.generated.h"

UCLASS()
class GAME1_API AComputerActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AComputerActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY()
		USceneComponent* Root;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UStaticMeshComponent* Mesh;

	UPROPERTY(BluePrintReadOnly)
		UDynamicTexture* mytex;

	//UMaterialInterface* MyMat;

	
};

source

// Fill out your copyright notice in the Description page of Project Settings.


#include "ComputerActor.h"

// Sets default values
AComputerActor::AComputerActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	RootComponent = Root;

	Mesh = CreateAbstractDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	Mesh->AttachTo(Root);
}

// Called when the game starts or when spawned
void AComputerActor::BeginPlay()
{
	Super::BeginPlay();
	
	//MyMat = Mesh->GetMaterial(2);

	mytex->Initialize(320, 200, FLinearColor::Blue);

	UMaterialInstanceDynamic* MyMatDynam = Mesh->CreateDynamicMaterialInstance(2);
	if (MyMatDynam != nullptr) {
		MyMatDynam->SetTextureParameterValue(FName("Moninp"), mytex->GetTextureResource());
	}
	//Mesh->SetMaterial(2, MyMatDynam);

		

	MyMatDynam->SetTextureParameterValue(TEXT("Moninp"), mytex->GetTextureResource());
}

// Called every frame
void AComputerActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


And do I use this with a material or a material instance