Compiling actor spawning code causing editor shutdown with c0000005 error

While testing actor runtime spawning, I’ve unfortunately encountered an error which I don’t understand at all.

I have a fully empty project except for AMyTestActor cpp and header files with a tiny bit of code within.
The code snippet:

SphereComponent = GetWorld()->SpawnActor<USphereComponent>(FVector(0.f, 0.f, 100.f), FRotator(0.f,0.f,0.f), FActorSpawnParameters());

if (SphereComponent) {
	SphereComponent->InitSphereRadius(100.f);
	SphereComponent->SetCollisionProfileName(UCollisionProfile::BlockAllDynamic_ProfileName);
	SphereComponent->SetHiddenInGame(false);
}

That’s all the handwritten code in the project, so there’s nothing else what can be the source of the error.

Compiling this code results in editor shutdown with error “Access violation - code c0000005 (first/second chance not available)”. Moveover, the editor can’t even launch until I delete builded and source files.

AFAIK this error is related to accessing wrong memory/null pointer, but I cannot understand what can cause the error here and why it happens in the editor, when it supposed to appear during the runtime.

What am I missing?

A USphereComponent isn’t an actor. It’s a UActorComponent which is meant to be attached to an actor. Actor classes begin with the letter A. AActor, APawn, ACharacter, etc. The first line of code will result in a run-time error because of this.

If you’re attempting to create a sphere component to attach to your test actor then you’d use CreateDefaultSubobject in the constructor.

Okay, I was sure I’ve done a very stupid mistake, and at least here I was right. Thanks.

No problem! We’ve all been there :slight_smile: