Crash after calling a function

Hey! :slight_smile: I am working on a function for my Weapon system. The problem is that when I call the “FireWeapon()” function, the Editor crashes with the following message:

I have no idea why it happense, but I am guessing that it has to do with some kind of code overload?

Here are all the calls associated with “FireWeapon()”:

void AFPSCharacter::OnFire()
	if (CurWP != NULL)
		const FVector cameraLocation = FirstPersonCameraComponent->GetComponentLocation();
		const FRotator cameraRotation = FirstPersonCameraComponent->GetComponentRotation();
		CurWP->InstPos = cameraLocation;
		CurWP->InstRot = cameraRotation;

void ADemoWeapon::FireWeapon()

	UWorld* const World = GetWorld();
	if (World != NULL)
		// Spawn the projectile at the viewpoint (camera) of the player
		World->SpawnActor<AFPSProjectile>(wpData.Projectile, InstPos, InstRot);

Anyone know what the problem could be? Help with this matter would be much appreciated :)) Thank you in advance! Feel free to ask any questions if something needs clarifying.

Hey Borzi!

A few things you might want to do, just to be sure:

In “OnFire()”, do a null check for your “FirstPersonCameraComponent”
(It’s not where the error is, but it’s good practice for pointers)

And secondly, the only issue I can see with “FireWeapon()” is that “wpData.Projectile”, could potentially be null.
So a check there would fix the issue, but then the issue of not having a projectile could arise.

Also, I’m assuming “wpData” is a struct, so make sure it’s a UPROPERTY, since garbage collection can be an issue.
(I’m not a fan of pointers inside structs. I tend to use “TWeakPtr” if I have to).

I would suggest a few On screen debugs to make sure you actually have a projectile. xD

I hope this helps!


Thank you very much for the reply, but im afraid that this did not solve the problem :frowning: There was indeed a projectile attached. Have you taken a look at the crash error? I don’t really think that the firing function has anything to do with it anymore. Maybe the declaration?

Here is what I have in my Weapon.h:

/** [local] weapon specific fire implementation */
	virtual void FireWeapon() PURE_VIRTUAL(AWeapon::FireWeapon, );

And here is what I have for the DemoWeapon.h:

virtual void FireWeapon() override;

Oh and by the way: What is WeakPtr? It would be kind of you to tell me, because i have seen it, but never understood how it works or is implemented.

You’re using garbage collection when you use C++ in Unreal. What that means is that your memory is managed for you, if you have a pointer to an object that object will stick around, if not then the memory can be freed for other things to use it.

A weak pointer lets you hold a reference to something without forcing it to stay in memory. You can use it to access that data as long as it sticks around, but if there are only weak pointers to an object then the garbage collector will be free to clean up the object anyway.

EDIT: Weak Pointers in Unreal Engine | Unreal Engine 5.2 Documentation

Thanks for the reply, that makes a lot more sense now :slight_smile: Anyone maybe also have a solution for the crash?

virtual void FireWeapon() PURE_VIRTUAL(AWeapon::FireWeapon, );

From your error log and your code, it tells you whats wrong. I don’t know what the PURE_VIRTUAL macro does, but I don’t think you want it. Pure Virtual functions are abstract functions that don’t have an implementation, which isn’t what you want.

Does your FireWeapon override another function, because if not, it shouldn’t be virtual either.

So what would you suggest instead? I need to have the “FireWeapon” override in my subclass, I just adapted this from the shooter example where they used this. Besides, doesn’t the function have to be of type virtual void so i can override it in the first place?

I haven’t looked at the example, so I’m not sure. I would try replacing that line with:

virtual void FireWeapon();

and see what happens.

That does not seem to work :confused: here is the error i get (I still have the same line to override the function in the DemoWeapon):

‘ADemoWeapon::FireWeapon’ : method with override specifier ‘override’ did not override any base class methods

It seems like I have to flag the function in the base class as being overrideable, but I don’t know how…

Can you post the .h files for both the parent class and the subclass?


Here is what I have in the base class:

/** [local] weapon specific fire implementation */
	virtual void FireWeapon() PURE_VIRTUAL(AInstinctWeapon::FireWeapon,);

Here is what I have in the sub class:

virtual void FireWeapon() override;

…and thats basically all I have for the FireWeapon() function

EDIT: I should note that I have tried the previously mentioned suggestions, but they resulted in a compile error.

I have found the solution. The problem was that I was calling the virtual void externally from a different script, which is not possible aparrently. I made the following function and then called it from my character script:

void AWeapon::ManageFire()
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Managing weapon"));
	if (bReadyToShoot())
			FireTimer = WeaponInfo.FireRate;
			if (WeaponInfo.UsesClip)
	if (bEmptyClip())
		if (bCanReload())

So Pure_Virtual is the way to go :slight_smile: