UDP sender socket stops working after 1 minute

I’m trying to create a sender/receive system with sockets and UDP, but with (almost) no luck.

I managed to make it working this way: there is a sender and there is a receiver. The sender sends data at each Tick, the receiver does something with it. I used as reference Rama’s tutorial: https://wiki.unrealengine.com/UDP_Socket_Sender_Receiver_From_One_UE4_Instance_To_Another

It works as expected, but after EXACTLY 1 the sender’s client crashes. It’s always 1 minute. I’ve no idea why. I know it has nothing to do with the data I’m sending (I always send the same thing, just for debug purposes)…

After 1 minute it crashes when the sender’s client calls the socket’s SendTo() method. Each time I print to the log this things (the socket’s reference and whether it’s null or not), to understand what’s happening:

 UE_LOG(LogTemp,Error,TEXT("socket: %d; is null: %d"), mSenderSocket, mSenderSocket == nullptr);

This is the first print out I get in the log:

[2017.11.11-16.00.20:302][593]LogTemp:Error: socket: 1345165696; is null: 0

As expected, the reference is a “random one” and it’s not null.

It goes like this for almost a minute, printing always the same thing. After 1 minute, this is what it prints:

[2017.11.11-16.01.27:901][317]LogTemp:Error: socket: -572662307; is null: 0

Notice that the reference has changed, but it’s not considered null.

Then it crashes, giving me this in the log:

 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xffffffff

My code is the same as Rama’s code, I’m not doing anything different. I’ve no idea why the socket changes address on its own though… I’m lost, can anyone help me out?

Found the solution. Was a silly garbage collection mistake: I was pretty sure everything that I was using was covered with a UPROPERTY(), but the instance of the UObject that I was using for the socket wasn’t. Adding UPROPERTY() to that instance fixxed the problem.

Good to know, thanks for sharing the solution!