# Exception when trying to bind a UFUNCTION with AddDynamic to a Delegate

**URL:** <https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192>\
**Category:** C++\
**Tags:** unreal-engine, question\
**Created:** [September 21, 2023, 9:04am UTC](https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192 "2023-09-21T09:04:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seeyouanteeius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@seeyouanteeius](https://forums.unrealengine.com/u/seeyouanteeius)\
**Post date:** [September 21, 2023, 9:04am UTC](https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192/1 "2023-09-21T09:04:11Z")

</div>

Hello.  
Added UDP Component plugin (from here: [GitHub - getnamo/UDP-Unreal: Convenience UDP wrapper for the Unreal Engine.](https://github.com/getnamo/UDP-Unreal/tree/master)) to my project and now im trying to bind a UFUNCTION HandleReceivedBytes (from my pawn class) to a Delegate event OnReceivedBytes (from this plugin).  
.h file:

```auto
class UAVCESIUM_API AUavSimPawn : public APawn
{
	GENERATED_BODY()

public:
	AUavSimPawn();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	// Utilities
	UFUNCTION()
	void HandleReceivedBytes(const TArray<uint8>& Bytes, const FString& IPAddress, const int32& Port);

	// Components
	UPROPERTY(VisibleAnywhere)
	class UUDPComponent* UdpComp;

```

.cpp file:

```auto

AUavSimPawn::AUavSimPawn()
{
	UdpComp = CreateDefaultSubobject<UUDPComponent>(TEXT("UdpComponent"));
	UdpComp->Settings.bShouldAutoOpenReceive = false;
	UdpComp->Settings.bShouldAutoOpenSend = false;
	UdpComp->OpenReceiveSocket(TEXT("127.0.0.1"), 5228);
}

void AUavSimPawn::BeginPlay()
{
	Super::BeginPlay();

	// Binding a function for handling OnReceivedBytes delegate
	// Exception happens here:
	UdpComp->OnReceivedBytes.AddDynamic(this, &AUavSimPawn::HandleReceivedBytes);

```

The excpetion happens in “ScriptDelegates.h” in this line (556):

```auto
(void)ensure( InvocationList[CurFunctionIndex] != InDelegate )

```

from this code:

```auto
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
		// Verify same function isn't already bound
		const int32 NumFunctions = InvocationList.Num();
		for( int32 CurFunctionIndex = 0; CurFunctionIndex < NumFunctions; ++CurFunctionIndex )
		{
			(void)ensure( InvocationList[CurFunctionIndex] != InDelegate ); // HERE
		}
#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
		InvocationList.Add( InDelegate );
	}

```

I figured out that this exception means that the function is already is in invokation list. Also i came to that point with debugger and yes, this function (“HandleReceivedBytes”) could be seen here before the function is actually added.  
But i couldn’t find out why does it happens - i only have one call of function “AddDynamic” (put a breakpoint on it and it happened only once).

P.s. Also guess i should mention this one: if I put “AddDynamic” line in ctor of my Pawn class then the exception doesn’t appear but when i play it creates another copy of my Pawn and binds that HandleReceivedBytes method to it instead of an instance of my Pawn which is on a level.

upd: when using AddUniqueDynamic instead of AddDynamic the error does not appear but the function bound to delegate does not invokes. I noticed something strange. I added those two lines to check if OnReceivedBytes delegate already has any bound functions and with debugger I found out that it really has one - it’s my HandleReceivedBytes. I can’t understand how it could be already bound if I’m binding it after this check:

```auto
void AUavSimPawn::BeginPlay()
{
    Super::BeginPlay();

    if (UdpComp->OnReceivedBytes.IsBound())
        UdpComp->OnReceivedBytes.Clear();
    //UdpComp->OnReceivedBytes.AddDynamic(this, &AUavSimPawn::HandleReceivedBytes);
    UdpComp->OnReceivedBytes.AddUniqueDynamic(this, &AUavSimPawn::HandleReceivedBytes); // Also tried AddUniqueDynamic just for overcoming the exception
    
}

```

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [September 21, 2023, 4:53pm UTC](https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192/2 "2023-09-21T16:53:26Z")

</div>

> [@seeyouanteeius](#):
>
> P.s. Also guess i should mention this one: if I put “AddDynamic” line in ctor of my Pawn class then the exception doesn’t appear but when i play it creates another copy of my Pawn and binds that HandleReceivedBytes method to it instead of an instance of my Pawn which is on a level.

binding to a delegate should definitely not be done in the constructor of any object. It should be done on an instance of a Pawn once when required, usually during BeginPlay. The constructor is only meant to set up the class itself without accessing other objects (can be dangerous). In UE there is another reason, read this:

> [What is CDO? - #2 by anonymous\_user\_9f4576f5](https://forums.unrealengine.com/t/what-is-cdo/310820/2)

This should live on BeginPlay as well:

```auto
UdpComp->OpenReceiveSocket(TEXT("127.0.0.1"), 5228);

```

It’s hard to tell what is going on without seeing an entire call stack. If you put a breakpoint before the moment it would assert, can you find new information in previous steps listed in the callstack?

---

<div class="post-metadata">

**Author:** ![seeyouanteeius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@seeyouanteeius](https://forums.unrealengine.com/u/seeyouanteeius)\
**Post date:** [September 22, 2023, 9:08am UTC](https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192/3 "2023-09-22T09:08:28Z")

</div>

> binding to a delegate should definitely not be done in the constructor of any object.

I did not bind to a delegate in a constructor, I put it in BeginPlay. Put thanks for a useful link!

> This should live on BeginPlay as well:
> 
> ```auto
> UdpComp->OpenReceiveSocket(TEXT("127.0.0.1"), 5228);
> 
> ```

Yep. It helped. Now I am able to listen to that socket.

> If you put a breakpoint before the moment it would assert, can you find new information in previous steps listed in the callstack?

Here is my call stack (I didn’t thinked out a better idea then just copy name of calls and put them in a list below):

- _This is top of stack where the exception happens:_ UE4Editor-Project.dll!TMulticastScriptDelegate::AddInternal::\_\_l4::\<lambda\_4de4e9647c80ad9f1ebd5845fb39e2ab\>::operator()() Line 556
- [Embedded frame] UE4Editor-Project.dll!TMulticastScriptDelegate::AddInternal(const TScriptDelegate &) Line 556
- [Embedded frame] UE4Editor-Project.dll!TMulticastScriptDelegate::Add(const TScriptDelegate &) Line 327
- UE4Editor-Project.dll!TBaseDynamicMulticastDelegate\<FWeakObjectPtr,void,TArray\<unsigned char,TSizedDefaultAllocator\<32\>\> const &,FString const &,int const &\>::\_\_Internal\_AddDynamic(AUavSimPawn \* InUserObject, void(AUavSimPawn::\*)(const TArray\<unsigned char,TSizedDefaultAllocator\<32\>\> &, const FString &, const int &) InMethodPtr, FName InFunctionName) Line 1110
- UE4Editor-Project.dll!AUavSimPawn::BeginPlay() Line 88

By some reasons there is still bounded function. Also I noticed one thing: even if I remove line with _AddDynamic_ or _AddUniqueDynamic_ unreal still binds HandleReceivedBytes to a delegate somehow…  
I tried cleaning and rebuilding the whole project but it didn’t change anything. Then I renamed my function which I’m trying to bind to another name and it suddenly works as expected: if this function is not bound to a delegate - nothing happens. And if I explicitly bind it with _AddDynamic_ then it successfully binds and the socket works.

I thought that inside the plugin there could be some default function with name _HandleReceivedBytes_ but there aren’t one.

Now everything works tho, but I still can’t understand how there could be already a bound function and how it could be bounded without using _AddDynamic_.

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [September 22, 2023, 6:48pm UTC](https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192/4 "2023-09-22T18:48:09Z")

</div>

> [@seeyouanteeius](#):
>
> Then I renamed my function which I’m trying to bind to another name and it suddenly works as expected: if this function is not bound to a delegate - nothing happens. And if I explicitly bind it with _AddDynamic_ then it successfully binds and the socket works.

I have a feeling that that happens using an ‘old’ blueprint asset deriving from the AUavSimPawn? Blueprints can corrupt like that. If your c++ is fine, it shouldn’t happen if your method name wasn’t stored somewhere in a way that conflicts with your current C++.

You could test making a new blueprint asset deriving from the class using the old function name, if that works you are most likely looking at a corrupt asset.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [October 22, 2023, 6:48pm UTC](https://forums.unrealengine.com/t/exception-when-trying-to-bind-a-ufunction-with-adddynamic-to-a-delegate/1306192/5 "2023-10-22T18:48:32Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
