Free the static multicast delegate memory?

I have this doubt.
I have created a static delegate in the GameMode to make it accessible on the client side (no authority)
And it is also accessible by other actors in the world.

The point is that I can’t use UPROPERTY.
So the garbage collector doesn’t take care of it.

And I see that it is a wrapped pointer (smart pointer).

But I’m not sure if in this case the memory is freed automatically. Should I free the memory using Reset()?

Thank you so much!!

There is no memory to free. I’m not sure what that weak ptr is, but it’s not something that you can reset to free up any memory. It’s a static variable so it will exist for the duration of the application and there’s nothing else you can do.

If it’s something for the client only, perhaps the gamestate is a better place for your delegate than a static in the game mode.

1 Like

OK, thank you very much for your comment.

WeakPtr is a Smart Pointer…

https://en.cppreference.com/book/intro/smart_pointers

In theory the memory in the class destructor is freed when it is no longer accessible. But I did not know its behavior when it is static or global. (maybe it’s never free as you say).

The thing is, I need to bind the delegate inside the OnPossessedPawnChanged() event.
In this event the clients have not authority.
Without authority I can’t get a reference to GameMode or GameState either.

That’s why I need a global/static event accessible to everyone. And especially in the Widget that I have in the HUD.

Thank you very much for your reply!!
Very appreciated!!

Getting a reference to the GameState has nothing to do with authority. You can get it regardless because it’s replicated to client. The GameMode instance is not, which is why you’d have a problem using it on a client. Further more you can absolutely track local, unreplicated state on the GameState. Just because you don’t have authority doesn’t mean you can’t modify it at all, it just means you can’t/shouldn’t modify replicated data.

Alternatively you could look at creating a LocalPlayerSubsystem to hold onto your delegate instead.

Or you could leave it alone. As is, it will do what you want (I think): it’s accessible to clients and to the other actors/objects of that client.
The garbage collector has a very narrow job, to manage the memory of UObject instances. This isn’t one of those (because it’s a static the fact that it’s declared inside a UObject type is irrelevant) so the garbage collector isn’t relevant.

Not maybe. Smart pointers (of any kind) manage the memory of object instances. Static variables are not part of the instance so their memory is unaffected by the use of smart pointers.

1 Like

It’s just what i need to know.
Thanks so much for the explanation.

GameState was definitely the best place.
The static delegate was a very bad idea.
Everything seemed to work fine until I realized that the program was running in a single process. So the static delegate in a multiplayer game would never work. So GameState is the quintessential site for “global” delegates.
Thanks for the idea and for the help! :heart:

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