# Function pointers in Unreal compared to standard C++. Getting an unexpected error which I cannot reproduce without UE4

**URL:** <https://forums.unrealengine.com/t/function-pointers-in-unreal-compared-to-standard-c-getting-an-unexpected-error-which-i-cannot-reproduce-without-ue4/441659>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [January 19, 2019, 9:07pm UTC](https://forums.unrealengine.com/t/function-pointers-in-unreal-compared-to-standard-c-getting-an-unexpected-error-which-i-cannot-reproduce-without-ue4/441659 "2019-01-19T21:07:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qvaLarsson](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@qvaLarsson](https://forums.unrealengine.com/u/qvaLarsson)\
**Post date:** [January 19, 2019, 9:07pm UTC](https://forums.unrealengine.com/t/function-pointers-in-unreal-compared-to-standard-c-getting-an-unexpected-error-which-i-cannot-reproduce-without-ue4/441659/1 "2019-01-19T21:07:05Z")

</div>

I tried to reproduce this issue in a non Unreal Engine project, but I can’t seem to do it, so I’m quite unsure how to solve this, you can see how I [tried to reproduce it here.][1]

Anyhow, I’m trying to write a triggering gameplay system which works like this:

You have a trigger, can be anything, a volume trigger, a written button class which trigger something, a timed event or anything. That said trigger calls its trigger option class which only exists for the possibility of different usecases, some options at the top of my head: proxytrigger which instantly triggers, delayedtrigger which delays the trigger process, or something more complex like a trigger option that will only give you the OK sign if your health is 100%.

Once the TriggerOption tells the real Trigger that it’s been triggered, the TriggerAction will play, which can also be anything, a door opening perhaps? I wonder how many times I’ve said “trigger” by now … anyway, that’s the entire system. However, the process of TriggerOption telling TriggerActor (the actual trigger) that it should get triggered happens through a function pointer:

This is the function which sets up the function pointer:

```
void ATriggerActor::AttemptToTrigger()
{
	if (!TriggerOption)
	{
		UE_LOG(LogTemp, Error, TEXT("TriggerOption is nullptr in TriggerActor: %s?"), *GetName());
		return;
	}

	if (CanBeTriggered())
	{
		TriggerOption->StartTriggerOption(this, &ATriggerActor::OnTriggerOptionCompletedCB);
		bHasBeenTriggered = true;
		TriggerCount++;
	}
}

```

This is the function that calls it, or stores it or whatever this subclass wants to do with it, in this case, it just calls it.

```
void UTriggerOptionProxy::StartTriggerOption(ATriggerActor* CallbackOwner, VoidFuncPtrNoParams CompletionCB)
{
	(CallbackOwner->* (CompletionCB))();
}

```

This is also the typdef for the function pointer btw: `typedef void(ATriggerActor::*VoidFuncPtrNoParams)(void);`

When trying to call the function pointer,

 ![265914-ue4error.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/e/a/fea0ccc141bfcafc0811c081a226d07cef4e63be.png)

, it throws this:

Exception thrown at 0x00007FF8A0954197 (UE4Editor-UE4Systems-3138.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

EDIT: Trying to fix the code format…

---

<div class="post-metadata">

**Author:** ![AmphDev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AmphDev](https://forums.unrealengine.com/u/AmphDev)\
**Post date:** [January 22, 2019, 2:53pm UTC](https://forums.unrealengine.com/t/function-pointers-in-unreal-compared-to-standard-c-getting-an-unexpected-error-which-i-cannot-reproduce-without-ue4/441659/2 "2019-01-22T14:53:28Z")

</div>

Hey,

Im not sure i understod fully what you want, but seems to me you try to overthink this a little bit 🙂

What about delegates, timers, events, interfaces? 🙂  
Technically all of this things is designed to trigger functions at some point and they implemented properly so you can use it freely…

I know this is not what you asked, but maybe a good advice 😉  
but if you wanna go with function pointers, maybe this wiki helps: [A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums](https://wiki.unrealengine.com/Function_Pointers)

> **[Delegates](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Delegates/)**
>
> Data types that reference and execute member functions on C++ Objects

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

> **[Using Delegates | Orfeas Eleftheriou](https://www.orfeasel.com/using-delegates/)**
>
> Delegates in C++ are used in a number of cases. However, in this post, think of delegates as pointers that point to specific functions. Having said that, through delegates we can call the functions that they are pointing to when…

> **[Events](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Delegates/Events/)**
>
> Delegates that can be bound to multiple functions and execute them all at once.

> **[Using C++ Timers in Unreal Engine](https://www.tomlooman.com/unreal-engine-cpp-timers/)**
>
> Unreal Engine Timers in C++. A quick tutorial on how to use timers and bind delegates with unreal engine in C++.

---

<div class="post-metadata">

**Author:** ![Knauric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Knauric](https://forums.unrealengine.com/u/Knauric)\
**Post date:** [May 3, 2023, 12:07am UTC](https://forums.unrealengine.com/t/function-pointers-in-unreal-compared-to-standard-c-getting-an-unexpected-error-which-i-cannot-reproduce-without-ue4/441659/3 "2023-05-03T00:07:33Z")

</div>

I realize this is quite an old topic, but I stumbled across this topic via Google and I am having the same problem as the OP. I have used function pointers like this in other code, but for some reason I cannot get it to work in Unreal Engine. I have a pointer to an owner (in my case, an object that derives from UActorComponent) and a pointer-to-member-function that points to a function that belongs to that owner’s class. But no matter how I do it, either invoking the function pointer directly or using std::invoke, an access violation exception is thrown similar to OP.

Hopefully someone can chime in with some helpful advice, because I’m feeling kind of stuck. The only interesting thing that I can point to to guide this discussion is that if I invoke the member function directly like this:

```
if( m_pOwner != nullptr && m_fpOwnerFunction != nullptr )
{
    ( m_pOwner->*m_fpOwnerFunction )( );
}

```

Then when the exception occurs, my call stack tells me that the exception is happening inside `TBaseUObjectMethodDelegateInstance::ExecuteIfSafe`. I don’t understand why this code would be executing since this section of code doesn’t reference delegates at all. I don’t see how this function call could possibly be going to a different place since I’m directly invoking a pointer-to-member-function (as opposed to potentially invoking an operator() overload that I’m unaware of).

I’ve been staring at this incredibly simple code for a few hours now and I have no idea why it won’t work. If anyone has any ideas, I would love to hear them.

---

<div class="post-metadata">

**Author:** ![Knauric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Knauric](https://forums.unrealengine.com/u/Knauric)\
**Post date:** [May 3, 2023, 1:58pm UTC](https://forums.unrealengine.com/t/function-pointers-in-unreal-compared-to-standard-c-getting-an-unexpected-error-which-i-cannot-reproduce-without-ue4/441659/4 "2023-05-03T13:58:14Z")

</div>

Well, I figured out my problem after a night’s sleep and some troubleshooting. I’ll post the answer here if anyone else ends up having my problem and is googling. My issue was that I forgot to include the header file that defined `m_pOwner`’s type in the source file that was invoking `m_fpOwnerFunction`. I thought the compiler would have caught a mistake like this since usually accessing incomplete types causes a compilation error, but it didn’t throw warnings or errors this time. The delegate code that was in my call stack turned out to be irrelevant.
