How to create a TSharedPtr?

I have a struct* that I’m trying to pass to a TSharedPtr. This is because I’m trying to send some data on to be computed on another thread, via FRunnable, and using a ‘normal’ pointer results in a crash.

I’m confused how to cast my ‘normal’ pointer to a TSharedPtr.

Right now, running


TSharedPtr<FRunnableContainer, ESPMode::ThreadSafe> ContainerPtr(NearCont);

with NearCont being an FRunnableContainer* (non-USTRUCT) causes a crash.

Furthermore, it seems to match the template for TSharedPtr, I get no red squiggly in VS, and I check (multiple times) to make sure that the pointer is non-null.

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/SmartPointerLibrary/
What you want is MakeShareable() function.
Class field (so it does not garbage-collected)


TSharedPtr<FRunnableContainer, ESPMode::ThreadSafe> ContainerPtr;

Where you create your Container:


ContainerPtr = MakeShareable(NearCont);

2 Likes

Thanks again RxDesu for the help, but unfortunately this still causes a crash for the same reason.

I now imagine this must be the result of my general workflow with the Runnable.

I have an Actor in the scene which is in charge of the runnable. In contains a list of AI characters in the scene who have needs to be fulfilled - Hunger, Thirst, Sleep, etc. So, every tick, this ‘manager’ actor chooses a character from its list, gets it’s ‘needs’ struct, and passes it on to the runnable. The runnable is created on BeginPlay, and while running will process the decay for those needs. It also checks to make sure the pointer IsValid().

Do you see where I went wrong at all?

There are several possible errors that could be made.

Provide some code samples for your pointer management. It will be helpful. (struct field declation in AICharacter, FRunnable method call and maybe that method implementation details)

Or you can try to take closer look into my abandoned (sigh) project https://github.com/RxGamesLtd/USS13:

  1. FluidSimulationManager - FRunnable
  2. WorldGrid - actor, that holds and controls that runnable
1 Like

Thank you!
Some things:

1.) You reference your FRunnable via a TSharedPtr - i.e [FONT=courier new]TSharedPtr<FMyRunnable> MyRunnable. I have it set up as [FONT=courier new]FMyRunnable* MyRunnable.

2.) You run the thread in a function called “Start”. Mine begins in the constructor.

3.) Your Thread is a [FONT=courier new]TSharedPtr as well. Mine is not.

4.) I have no Init() function, nor a Stop() function. Even if that isn’t causing the crash, that can obviously become a problem.

I’m going to check all of those and see if maybe one or more is responsible for the crash, but if you see any that look particularly evil let me know.

Here is the code pertinent to the error, and then some: Clone - Google Drive
The actual line that causes the crash is Line 23 in Runnable Manager.