Access violation executing location on 2nd run

I have a chunk of code that works flawlessly… Once. If I run it a second time I get “Access violation executing location” error.

This function is called many times, and only errors when the Function is “compare”, on the 2nd execution.
Sometimes the declaration of Function results in “Invalid name length”, sometimes not.
Sometimes the “function” is correct, but the "FunctionNodeSpawner " declaration results in a nullptr.
Some times it results in no pins.

It’s like I’m failing to do some clean-up somewhere… but I thought that was all automatic.
Note: The program creates a file, and I have to delete this file before running it again.

if (ComparePinType != “None”) is only true when Function = “Compare”

UEdGraphNode* UMakeBlueprintNodeBPLibrary::AddFunctionNodeToBlueprint(UEdGraph* Graph, FName FunctionName, UClass* Class, FVector2D Location, FName ComparePinType)
{
if (!Graph)
{
// Blueprint is null, handle the error
return nullptr;
}

	if (!Class)
	{
		// The blueprint class is null, handle the error
		return nullptr;
	}

	
	UFunction* Function = Class->FindFunctionByName(FunctionName);
	if (!Function)
	{
		// The function does not exist in the blueprint class, handle the error
		UE_LOG(LogTemp, Warning, TEXT("Function Not Found"));
		return nullptr;
	}

	// Get the Event Graph of the blueprint
	//UEdGraph* EventGraph = Blueprint->UbergraphPages[0];

	// Create a new function node in the Event Graph
	UEdGraphNode* NewNode = NewObject<UEdGraphNode>(Graph);


	UBlueprintFunctionNodeSpawner* FunctionNodeSpawner = UBlueprintFunctionNodeSpawner::Create(Function);
	if (FunctionNodeSpawner)
	{
		FunctionNodeSpawner->SetFlags(RF_Transactional);
		NewNode = FunctionNodeSpawner->Invoke(Graph, IBlueprintNodeBinder::FBindingSet(), FVector2D(Location.X, Location.Y));
		if (ComparePinType != "None")
		{
			TArray<UEdGraphPin*> Pins;
			Pins = NewNode->GetAllPins();
			if (Pins.Num() > 0)
			{
				for (UEdGraphPin* Pin : Pins)
				{
					if (Pin->PinName == FName(TEXT("A")) || Pin->PinName == FName(TEXT("B")))
					{
						if (ComparePinType == FName(TEXT("Vector")) || ComparePinType == FName(TEXT("vector")))
						{
							Pin->PinType.PinCategory = UEdGraphSchema_K2::PC_Struct;
							Pin->PinType.PinSubCategoryObject = TBaseStructure<FVector>::Get();
						}
						else if (ComparePinType == FName(TEXT("double")))
						{
							Pin->PinType.PinCategory = TEXT("real");
							Pin->PinType.PinSubCategory = ComparePinType;
						}
						else if (ComparePinType == FName(TEXT("color")))
						{
							Pin->PinType.PinCategory = UEdGraphSchema_K2::PC_Struct;
							Pin->PinType.PinSubCategoryObject = TBaseStructure<FColor>::Get();
						}
						else
						{
							Pin->PinType.PinCategory = ComparePinType;
						}
					}
				}
			}
		}
		return NewNode;
	}

	return nullptr;
}

If I remove the one call that has the function “compare”, it runs fine any number of times.
With one call to this function with “compare” it works fine once. Always crashes on 2nd run.

Even more bizarre:
This function is being called by a Blueprint node.

If I disconnect this node’s input exec, and compile, it runs unlimited number of times.
Reconnect, it runs once then crash.

BUT

If I disconnect the both the input and exec pins, compile, run, connect both back up… and it runs fine an unlimited number of times.

Still have this problem.
And now I have the " Access violation executing location on 2nd run" but in a 2nd area.
-_-

Anyone have any idea why it would work when I hit compile once… then give “Access violation” when I hit compile a 2nd time? “Insanity, doing the same thing an expecting a different result”… Yet… I’m doing the same thing twice and getting two different results. :frowning:

Nearly a year later… still haven’t figure out how to fix this… -_-
Would love some help.