Getting an access violation while multi-threading and I don't know why

Hello dear community,

I am currently writing my own .stl importer for importing meshes at runtime. For performance purposes, I’d like to fire the importer in another thread. The thread itself is working, and I already get correct positions and facenumbers printed out in the log. However, at the end of the process, I am getting an access violation error and I have no clue what might be causing this. I would really appreciate every bit of help over here.

That’s the code of the .cpp:

#include "ArchitectureTool.h"
#include "LoadSTLMeshThread.h"


LoadSTLMeshThread::LoadSTLMeshThread(FString PathToObject, UCustomMeshComponent* customMeshPtr)
{
	m_PathToObject = PathToObject;
	m_MeshComponentPtr = customMeshPtr;
	Thread = FRunnableThread::Create(this, TEXT("STL_MESH_IMPORT_THREAD"));
	
}

LoadSTLMeshThread::~LoadSTLMeshThread()
{
}

uint32 LoadSTLMeshThread::Run()
{
	/** Import .stl-ASCII **/
	FString FileData = "";
	FFileHelper::LoadFileToString(FileData, *m_PathToObject);
	TArray<FString> lines;
	FileData.ParseIntoArrayLines(lines);
	/** Extract vertex positions from the lines and add them to a TArray<FVector*>. Create a FCustomMeshTriangle every 3 vertecies. **/

	TArray<FVector*> TriangleVerts;
	int32 vertCount = 0;
	FString cmpLine = " ";
	int posCounter = 0;
	TArray<FCustomMeshTriangle*> _listToAppend;
	for (FString line : lines) {


		if (line.Contains("vertex")) {
			while (line.GetCharArray()[0] == cmpLine[0]) {
				line.RemoveAt(0);
			}
			FString leftValHolder;
			TArray<FString> VertCoords;
			line.RemoveFromStart("vertex ");
			line.Split(cmpLine, &leftValHolder, &line);
			FVector VertexPos = FVector();
			VertexPos.X = FCString::Atof(*leftValHolder);
			line.Split(cmpLine, &leftValHolder, &line);
			VertexPos.Y = FCString::Atof(*leftValHolder);
			VertexPos.Z = FCString::Atof(*line);

			TriangleVerts.Add(&VertexPos);
			vertCount++;
			
			lines.Remove(line);
			if (vertCount == 3)
			{

				FCustomMeshTriangle CustomMeshTriangle = FCustomMeshTriangle();

				CustomMeshTriangle.Vertex0 = *TriangleVerts[0];
				CustomMeshTriangle.Vertex1 = *TriangleVerts[1];
				CustomMeshTriangle.Vertex2 = *TriangleVerts[2];
				_listToAppend.Add(&CustomMeshTriangle);
				UE_LOG(LogTemp, Log, TEXT("Vertex 1 position: %s"), *TriangleVerts[0].ToString());
				UE_LOG(LogTemp, Log, TEXT("Vertex 2 position: %s"), *TriangleVerts[1].ToString());
				UE_LOG(LogTemp, Log, TEXT("Vertex 3 position: %s"), *TriangleVerts[2].ToString());
				TriangleVerts.Empty();
				vertCount = 0;
				posCounter++;
				UE_LOG(LogTemp, Log, TEXT("Number of faces: %d"), posCounter);
			}
		}


	}


	return uint32();
}

And that’s the .h file:
// Fill out your copyright notice in the Description page of Project Settings.
#include “CustomMeshComponent.h”
#include “ArchitectureTool.h”
#pragma once

/**
 * 
 */
class ARCHITECTURETOOL_API LoadSTLMeshThread : public FRunnable
{
public:
	LoadSTLMeshThread(FString pathToObject, UCustomMeshComponent* customMeshComponentPtr);
	~LoadSTLMeshThread();

	UPROPERTY()
	FString m_PathToObject;
	UPROPERTY()
	FRunnableThread* Thread;
	UPROPERTY()
	UCustomMeshComponent* m_MeshComponentPtr;

	// Inherited via FRunnable
	virtual uint32 Run() override;
};

Does anybody see what I am doing wrong?

Best wishes,

I’m having a similar issue, trying to return a value from another thread is causing the crash.

Try to return 0, not uint32()

It looks like you’re adding the address of a local stack variable to your array each time:

_listToAppend.Add(&CustomMeshTriangle);

That array is just going to be filled with bogus pointers when you’re done (although it doesn’t look like you’re actually using it?).

I’d consider changing that to be a value type. Also, accessing UCustomMeshComponent* m_MeshComponentPtr on your thread is not advisable unless you have guaranteed its lifetime on the main thread, along with thread-safe access.

Hope that helps.