Download

Game crash sometimes on loading object.

Dear UT4 community,

I’m calling


static UBlueprint* nAssetBlueprint = Cast<UBlueprint>(StaticLoadObject(UBlueprint::StaticClass(), NULL, *nAssetPath));
if (nAssetBlueprint ) GetWorld()->SpawnActor<TYPE>(nAssetBlueprint->GeneratedClass, nPos, nDir.Rotation(), nSpawnParams);

and sometimes the engine crashes with this error (like 5% chance)


Access violation - code c0000005 (first/second chance not available)

UE4Editor-CoreUObject.dll

[Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xffffffff]

Anyone got the same experience or know any cure?

Best,

Why is that variable declared ‘static’? Try removing that. Also, did you read the Unreal documentation about smart pointers & about Unreal garbage collection? Granted, if the garbage collector got ya you should have died with a much more gentle NULL pointer exception if memory recalls (and you would have died a few ticks later, not instantly…)

The way you’re spawning from a blueprint is nothing like how I’ve done it.

Normally I expose a class type to the editor like so



	// List of Character stats belonging to this character
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Config)
		TSubclassOf<class UCharacterStats> CharacterStatsClass;

	// Stores all the attributes for the character
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = CharacterStats)
	UCharacterStats* Stats;


Within my .cpp when I am ready to create the object I do this:



	if (CharacterStatsClass)
	{
		Stats = Cast<UCharacterStats>(StaticConstructObject(CharacterStatsClass));
		if (Stats)
		{


Setting up your pointers as uproperty ensures it will not be garbage collected. Granted, I don’t think that’s your problem. I do think it’s odd to see a static pointer pointing at a UObject. Havent had time to look at Unreal’s Memory management systems. So no idea if that is allowed or not.

edit- Btw, the reason why your crash will appear random is because it all depends on the contents of the pointer you are using. Try looking at the pointer in the debugger. See if you see any trash in the object (you know, like impossible values assigned to a boolean, etc). You’ve no doubt stumbled unto a bug that has undefined results.

The reason for the round-about code is that we need to dynamically load and cast many object type that was created and saved as blueprints. We declared static b/c we’re using the StaticLoadObject() to load our blueprints… but guess that was unnecessary. I’ll try removing them, will let you know if this fix it.

Thx a million. :slight_smile: