Adding to multi-cast delegate causing crash

I have declared a multicast delegate like so:


FDelegateType MyDelegate;

but when I try to add a function to this delegate

MyDelegate.AddUObject(this, &SomeClass::SomeFunction);

it causes Unreal to crash with the following error:

Access violation - code c0000005 (first/second chance not available)

UE4Editor_Unreal!FMulticastDelegateBase<FWeakObjectPtr>::CompactInvocationList() [c:\program files\epic games\ue_4.18\engine\source\runtime\core\public\delegates\multicastdelegatebase.h:159]
UE4Editor_Unreal!FMulticastDelegateBase<FWeakObjectPtr>::AddInternal() [c:\program files\epic games\ue_4.18\engine\source\runtime\core\public\delegates\multicastdelegatebase.h:146]
UE4Editor_Unreal!TBaseMulticastDelegate<void>::AddUObject<UHUDScoreBar>() [c:\program files\epic games\ue_4.18\engine\source\runtime\core\public\delegates\delegatesignatureimpl.inl:837]

Specifically, the variable InvocationListLockCount (a mutable int) in MulticastDelegateBase.h has a null value.

It seems as though the delegate has not been initialized, but I am unsure why.

Any help would be appreciated.

Where/when are you binding the delegate?

See this:

It seems as though the delegate has not been initialized, but I am unsure why.

If you’re binding the delegate in the start up module of your plugin, then you can solve the issue by loading your plugin module later. Go to the uplugin file of your plugin and change the LoadingPhase to PostEngineInit. This change let me bind the OnLevelActorDeleted delegate in my start up module function. Without this change I’d also get an error with InvocationListLockCount.

I had a similar issue where editor crashed within CompactInvocationList()

The solution for me was to make sure my function had a UFUNCTION() macro above it (it doesn’t need to specify anything, just exist), hopefully this info helps someone else who encounters a crash in this function.