# Creating a onetime lambda function bind to a delegate

**URL:** <https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [November 21, 2024, 6:54pm UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395 "2024-11-21T18:54:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrm\_dev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mrm\_dev](https://forums.unrealengine.com/u/mrm_dev)\
**Post date:** [November 21, 2024, 6:54pm UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/1 "2024-11-21T18:54:11Z")

</div>

How do I bind a lambda function such that it get’s invoked ONLY once and then unbinds?

I tried this but it doesn’t work:

```cpp

DECLARE_MULTICAST_DELEGATE(FMyDelegate);

FMyDelegate MyDelegate;

FDelegateHandle DelegateHandle;
DelegateHandle=MyDelegate.AddLambda([this, &DelegateHandle](){

    ...

    bool WasRemoved = MyDelegate.Remove(DelegateHandle); // returns false
});

```

I something like this possible / feasible or should I look into something else entirely?

---

<div class="post-metadata">

**Author:** ![MagForceSeven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/magforceseven/32/906563_2.png) [@MagForceSeven](https://forums.unrealengine.com/u/MagForceSeven)\
**Post date:** [November 21, 2024, 8:08pm UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/2 "2024-11-21T20:08:35Z")

</div>

You’d have to make the delegate handle a member of ‘this’ instead of a local variable.

You’re capturing a reference to stack data, and when that variable goes out of scope then the reference in your lambda will be invalid.

You may also be able to do a TSharedPtr as a local variable and capture that (by value). Because then the handle won’t go out of scope when the function ends.

---

<div class="post-metadata">

**Author:** ![silnarm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/silnarm/32/955303_2.png) [@silnarm](https://forums.unrealengine.com/u/silnarm)\
**Post date:** [November 22, 2024, 4:26am UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/3 "2024-11-22T04:26:37Z")

</div>

Simpler solution since an FDelegateHandle is just an int, drop the `&` and pass by value.

---

<div class="post-metadata">

**Author:** ![mrm\_dev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mrm\_dev](https://forums.unrealengine.com/u/mrm_dev)\
**Post date:** [November 22, 2024, 4:31am UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/4 "2024-11-22T04:31:10Z")

</div>

Nope that does not work I tried it, However @MagForceSeven 's answer works when making the delegate handle a member instead of using a local variable

though I’m trying to figure out how to use TSharedPtr so I can use it as a local variable

---

<div class="post-metadata">

**Author:** ![mrm\_dev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mrm\_dev](https://forums.unrealengine.com/u/mrm_dev)\
**Post date:** [November 22, 2024, 4:53am UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/5 "2024-11-22T04:53:56Z")

</div>

making the delegate handle a member works but Im trying to keep my code clean and use a local variable instead

I tried your TSharedPtr solution like this:

```cpp
	TSharedPtr<FDelegateHandle> DelegateHandle = MakeShared<FDelegateHandle>();

	*DelegateHandle=MyDelegate.AddLambda([this, DelegateHandle](){

		bool WasRemoved = MyDelegate.Remove(*DelegateHandle); // returns true
	});

```

But I’m fairly new to `TSharedPtr` Will this cause any memory leaks? Also are there any edge cases where this might not work ?

---

<div class="post-metadata">

**Author:** ![silnarm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/silnarm/32/955303_2.png) [@silnarm](https://forums.unrealengine.com/u/silnarm)\
**Post date:** [November 22, 2024, 4:55am UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/6 "2024-11-22T04:55:45Z")

</div>

Indeed, my bad. `DelegateHandle` has not been initialised at the point it is passed, so a reference or pointer will be required.

---

<div class="post-metadata">

**Author:** ![mrm\_dev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mrm\_dev](https://forums.unrealengine.com/u/mrm_dev)\
**Post date:** [November 22, 2024, 6:45am UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/7 "2024-11-22T06:45:58Z")

</div>

Also is this the right way to create a static function to bind it ?

```cpp
template <typename DelegateType, typename FunctionType>
static void OnetimeMulticastDelegate(DelegateType &Delegate, FunctionType &&Lambda ){

	TSharedPtr<FDelegateHandle> DelegateHandle = MakeShared<FDelegateHandle>();
    
    *DelegateHandle = Delegate.AddLambda([DelegateHandle, &Delegate, Lambda]()
    {
        Lambda();
        
        if (DelegateHandle.IsValid())
             Delegate.Remove(*DelegateHandle);
    });
}

// usage
OnetimeMulticastDelegate(MyDelegate, [](){
   ...
})

```

---

<div class="post-metadata">

**Author:** ![MagForceSeven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/magforceseven/32/906563_2.png) [@MagForceSeven](https://forums.unrealengine.com/u/MagForceSeven)\
**Post date:** [November 22, 2024, 2:53pm UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/8 "2024-11-22T14:53:15Z")

</div>

> [@mrm\_dev](#):
>
> Will this cause any memory leaks?

No. When the delegate is removed, the lambda will be destructed which will destruct your copy of the shared ptr and the handle it was referencing will be free’d.

> [@mrm\_dev](#):
>
> Also are there any edge cases where this might not work ?

Not that come to mind.

> [@mrm\_dev](#):
>
> Also is this the right way to create a static function to bind it ?

That looks like it should be fine. You have to be careful when capturing stuff (like that delegate) by reference but since there’s no way for the Added lambda to be called if the delegate no longer exists you should be fine.

---

<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:** [December 22, 2024, 2:53pm UTC](https://forums.unrealengine.com/t/creating-a-onetime-lambda-function-bind-to-a-delegate/2141395/9 "2024-12-22T14:53:31Z")

</div>

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