Moving a TArray of pointers from BeginPlay() to outside of the function (global) crashes the engine?

If it crashes on that declertion in global that means there problem during initiation of std::vector for some reason. Did you try moving it to class deceleration so it will be part of actor class? As long as you won’t use UPROPERTY() on it UE4 should not mind it, UObject classes are still normal C++ classes.

Generly if you making some library support for UE4 best practice is to wrap it’s API in to UE4 APIs and then use it elsewhere, instead of direly communicating with it

Also do you really need to use std::vector? By look of it you could use TArray too, again without UPROPERTY() this should not be a issue, TArray will simply store pointers

i updated comment with some other possible solution ;] I will also convert it to anwser i guess

A snippet of the code in question is below. The vector, devices, is what is giving me grief. I need it to be outside of BeginPlay() because I need to use the pointers in the vector when Update() is called, but when I move it out of BeginPlay(), the engine crashes when I hit play :confused: I am using a third party library called librealsense.

TArray<rs::device *> devices; // this is the line moved from BeginPlay() that crashes the engine.

	PrimaryActorTick.bCanEverTick = true;

void ARealsenseData::BeginPlay()

	rs::context ctx;
	if (ctx.get_device_count() == 0) print("No device detected. Is it plugged in?");

	// Enumerate all devices
	//TArray<rs::device *> devices;

	for (int i = 0; i<ctx.get_device_count(); ++i)

	// Configure and start our devices
	for (auto dev : devices)
		dev->enable_stream(rs::stream::depth, rs::preset::best_quality);
		dev->enable_stream(rs::stream::color, rs::preset::best_quality);

void ARealsenseData::Tick( float DeltaTime )
	Super::Tick( DeltaTime );

/*	for (auto dev : devices)
	} */

Are you suggesting I add std::vector devices to the Actor base class? I have not tried that. Will give it a shot! Thanks.

I have actually tried swapping the vector with Tarray and got the same crash when I moved the Tarray outside of beginplay() :confused:

Ahh ok, try placing in class

I’ll give it a try later tonight and let you know how it goes. thanks.

Ah i also find other issue, you should not use exceptions in UE4 ( see “throw”) they can mess up things as UE4 dont expect them being used. Either just make some behavioir in case of error or use check(bool condition here); which will stop the engine if condition is false, failed condition will be printed in log. But i think check() is little too radical in this case, you should inform user that be informed that device is not connected.

Thanks for the advice! I’ll change that.

I placed the devices TArray into my class’s .h file and that fixed the crashing! Thanks!