Delegates and Threadsafety

I have i class with a DYNAMIC_MULTICAST_DELEGATE_OneParam, I have linked this to an event, see picture.

But as soon as I want to spawn an actor, Visual Studio reports “A breakpoint instruction (__debugbreak() statement or a similar call) was executed in UnrealEditor.exe.”

I think I’m in the wrong thread if I want to spawn the Actor. A picture of the CallStack.

How can I make this threadsafe?

try using
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam

USTRUCT()
struct FAlbumLoaded
{
	GENERATED_BODY()

public:

	UPROPERTY(EditAnywhere)
	FString title;
};


DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FAlbumDelegate, FAlbumLoaded, PassedAlbum);

called it with function in cpp

void AActorDeleg::Call()
{
	FAlbumLoaded album;
	album.title = TEXT("Test");
	OnSQLLiteAlbumLoaded.Broadcast(album);
}

No errors.

If you are using threads (via FRunnnable) then you need to call spawn within the game thread ONLY.
You can use thread safe containers like Queues (TQueue) to pass data.
Then in tick you can check the data => read the data from the queue => dequeue it.

The queue is thread-safe in both modes. The [Dequeue()](https://docs.unrealengine.com/5.3/en-US/API/Runtime/Core/Containers/TQueue/Dequeue) method ensures thread-safety by writing it in a way that does not depend on possible instruction reordering on the CPU. The [Enqueue()](https://docs.unrealengine.com/5.3/en-US/API/Runtime/Core/Containers/TQueue/Enqueue/1) method uses an atomic compare-and-swap in multiple-producers scenarios.
1 Like

I use a FNonAbandonableTask, sorry, i forget to say this important thing. In the SQLiteAlbumDTO i have a pointer to the SQLite instance and i am using this to send to broadcast.

class SQLiteAlbumDTO : public FNonAbandonableTask
{
// Code reduced to the minimum
	FORCEINLINE TStatId GetStatId()
	{
		RETURN_QUICK_DECLARE_CYCLE_STAT(SQLiteAlbumDTO, STATGROUP_ThreadPoolAsyncTasks);
	}
	void DoWork();
};

//static blueprint library ↓

	(new FAutoDeleteAsyncTask<SQLiteAlbumDTO>(me, id))->StartBackgroundTask();

SQLiteAlbumDTO::~SQLiteAlbumDTO()
{
	Owner->OnSQLiteAlbumLoaded.Broadcast(Result);
}

That’s why and because of the entry in the CallStack I thought it was a thread problem.

I will now try this with TQueue.

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