C++ code doesn't execute past a certain point

Hi! I’ve been trying to make an UDP connection for receiving and sending data following Rama’s tutorial; and I’ve managed to do it mostly, except for when it comes to closing connections.

I initialize listen socket like this:

bool AUDPConnector::StartUDPReceiver(
	const FString& YourChosenSocketName,
	const FString& TheIP,
	const int32 ThePort
)
{
	FIPv4Address Addr;
	FIPv4Address::Parse(TheIP, Addr);

	//Create Socket
	FIPv4Endpoint Endpoint(Addr, ThePort);

	//BUFFER SIZE
	int32 BufferSize = 2 * 1024 * 1024;

	ListenSocket = FUdpSocketBuilder(*YourChosenSocketName).AsNonBlocking().AsReusable().BoundToEndpoint(Endpoint).WithReceiveBufferSize(BufferSize);

	FTimespan ThreadWaitTime = FTimespan::FromMilliseconds(100);
	UDPReceiver = new FUdpSocketReceiver(ListenSocket, ThreadWaitTime, TEXT("UDP RECEIVER"));
	UDPReceiver->OnDataReceived().BindUObject(this, &AUDPConnector::Recv);
	UDPReceiver->Start();

	return true;
}

My code for closing the connection looks like this:

void AUDPConnector::CloseConnections()
{
	if (UDPReceiver != nullptr)
	{
		delete UDPReceiver;
		UDPReceiver = nullptr;
	}

	if (ListenSocket)
	{
		ListenSocket->Close();
		ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ListenSocket);
	}

	if (SenderSocket)
	{
		SenderSocket->Close();
		ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(SenderSocket);
	}
}

For some reason the code doesn’t execute past delete UDPReceiver line, so sockets aren’t closed and UDPReceiver isn’t null and it causes unreal to hang when I “close” the connection. I’ve been stuck on this for couple of hours now and I’m totally lost on what to make of it. Halp?

I you’re using breakpoints to see whether a line of code is being executed make sure you are running Debug configuration instead of development as with development configuration compiler optimizations may trick the debugger.