Search Unity

Resolved NGO + Relay Errors and Memory Leaks

Discussion in 'Multiplayer' started by Exominem, May 6, 2023.

  1. Exominem

    Exominem

    Joined:
    Nov 11, 2019
    Posts:
    4
    I've been working on a simple game with basic 2d movement and networking for about a week now. I got NGO working and I added Relay, both with a good number of troubles, but I'm at a point where it works. However, whenever I start a host, there's two errors which don't really make any sense to me. These two errors seem to be directly responsible for two memory leaks as well. It's still perfectly playable, and after the first two errors it tries again successfully, but I want to fix this issue before I move on. I've tried many different things and none of them have worked. For reference, here's the code along with the full four errors:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using Unity.Netcode;
    5. using Unity.Netcode.Transports.UNET;
    6. using Unity.Netcode.Transports.UTP;
    7. using Unity.Networking.Transport.Relay;
    8. using Unity.Services.Authentication;
    9. using Unity.Services.Core;
    10. using Unity.Services.Relay;
    11. using Unity.Services.Relay.Models;
    12. using UnityEngine;
    13. using UnityEngine.UI;
    14.  
    15. public class RelayTest : MonoBehaviour
    16. {
    17.     [SerializeField] private Button hostButton;
    18.     [SerializeField] private Button joinButton;
    19.     [SerializeField] private TMP_InputField codeJoin;
    20.  
    21.     private void Awake()
    22.     {
    23.         hostButton.onClick.AddListener(() =>
    24.         {
    25.             CreateRelay();
    26.         });
    27.         joinButton.onClick.AddListener(() =>
    28.         {
    29.             JoinRelay(codeJoin.text);
    30.         });
    31.     }
    32.  
    33.     private async void Start()
    34.     {
    35.         await UnityServices.InitializeAsync();
    36.         AuthenticationService.Instance.SignedIn += () =>
    37.         {
    38.             Debug.Log("Signed in: " + AuthenticationService.Instance.PlayerId);
    39.         };
    40.         await AuthenticationService.Instance.SignInAnonymouslyAsync();
    41.     }
    42.  
    43.     private async void CreateRelay()
    44.     {
    45.         try
    46.         {
    47.             Allocation allocation = await RelayService.Instance.CreateAllocationAsync(9);
    48.  
    49.             string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
    50.  
    51.             Debug.Log(joinCode);
    52.  
    53.             RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
    54.  
    55.             NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
    56.  
    57.             NetworkManager.Singleton.StartHost();
    58.         } catch (RelayServiceException err) {
    59.             Debug.Log(err);
    60.         }
    61.     }
    62.  
    63.     private async void JoinRelay(string joinCode)
    64.     {
    65.         try
    66.         {
    67.             Debug.Log("Joining relay with: " + joinCode);
    68.             JoinAllocation joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
    69.  
    70.             RelayServerData relayServerData = new RelayServerData(joinAllocation, "dtls");
    71.  
    72.             NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
    73.  
    74.             NetworkManager.Singleton.StartClient();
    75.         } catch (RelayServiceException err) {
    76.             Debug.Log(err);
    77.         }
    78.     }
    79. }
    Error #1: You must call SetRelayServerData() at least once before calling StartRelayServer.

    Error #2: Server is shutting down due to network transport start failure of UnityTransport!

    Memory Leak 1
    A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    Unity.Networking.Transport.NetworkSettings:.ctor(Allocator) (at Library\PackageCache\com.unity.transport@1.3.4\Runtime\NetworkSettings.cs:63)
    Unity.Netcode.Transports.UTP.UnityTransport:Initialize(NetworkManager) (at Library\PackageCache\com.unity.netcode.gameobjects@1.4.0\Runtime\Transports\UTP\UnityTransport.cs:1190)
    Unity.Netcode.NetworkManager:Initialize(Boolean) (at Library\PackageCache\com.unity.netcode.gameobjects@1.4.0\Runtime\Core\NetworkManager.cs:833)
    Unity.Netcode.NetworkManager:StartHost() (at Library\PackageCache\com.unity.netcode.gameobjects@1.4.0\Runtime\Core\NetworkManager.cs:953)
    <>c:<Awake>b__2_0() (at Assets\Scripts\NetManUI.cs:16)
    UnityEngine.Events.InvokableCall:Invoke()
    UnityEngine.Events.UnityEvent:Invoke()
    UnityEngine.UI.Button: Press() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\UI\Core\Button.cs:70)
    UnityEngine.UI.Button:OnPointerClick(PointerEventData) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\UI\Core\Button.cs:114)
    UnityEngine.EventSystems.ExecuteEvents:Execute(IPointerClickHandler, BaseEventData) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\ExecuteEvents.cs:57)
    UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\ExecuteEvents.cs:272)
    UnityEngine.EventSystems.StandaloneInputModule:ReleaseMouse(PointerEventData, GameObject) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:190)
    UnityEngine.EventSystems.StandaloneInputModule: ProcessMousePress(MouseButtonEventData) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:640)
    UnityEngine.EventSystems.StandaloneInputModule: ProcessMouseEvent(Int32) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:546)
    UnityEngine.EventSystems.StandaloneInputModule: ProcessMouseEvent() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:526)
    UnityEngine.EventSystems.StandaloneInputModule: Process() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:280)
    UnityEngine.EventSystems.EventSystem:Update() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\EventSystem.cs:514)


    Memory Leak 2
    A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    Unity.Collections.NativeList`1:.ctor(AllocatorHandle) (at Library\PackageCache\com.unity.collections@1.4.0\Unity.Collections\NativeList.cs:106)
    Unity.Networking.Transport.NetworkSettings:.ctor(Allocator) (at Library\PackageCache\com.unity.transport@1.3.4\Runtime\NetworkSettings.cs:62)
    Unity.Netcode.Transports.UTP.UnityTransport:Initialize(NetworkManager) (at Library\PackageCache\com.unity.netcode.gameobjects@1.4.0\Runtime\Transports\UTP\UnityTransport.cs:1190)
    Unity.Netcode.NetworkManager:Initialize(Boolean) (at Library\PackageCache\com.unity.netcode.gameobjects@1.4.0\Runtime\Core\NetworkManager.cs:833)
    Unity.Netcode.NetworkManager:StartHost() (at Library\PackageCache\com.unity.netcode.gameobjects@1.4.0\Runtime\Core\NetworkManager.cs:953)
    <>c:<Awake>b__2_0() (at Assets\Scripts\NetManUI.cs:16)
    UnityEngine.Events.InvokableCall:Invoke()
    UnityEngine.Events.UnityEvent:Invoke()
    UnityEngine.UI.Button: Press() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\UI\Core\Button.cs:70)
    UnityEngine.UI.Button:OnPointerClick(PointerEventData) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\UI\Core\Button.cs:114)
    UnityEngine.EventSystems.ExecuteEvents:Execute(IPointerClickHandler, BaseEventData) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\ExecuteEvents.cs:57)
    UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\ExecuteEvents.cs:272)
    UnityEngine.EventSystems.StandaloneInputModule:ReleaseMouse(PointerEventData, GameObject) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:190)
    UnityEngine.EventSystems.StandaloneInputModule: ProcessMousePress(MouseButtonEventData) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:640)
    UnityEngine.EventSystems.StandaloneInputModule: ProcessMouseEvent(Int32) (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:546)
    UnityEngine.EventSystems.StandaloneInputModule: ProcessMouseEvent() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:526)
    UnityEngine.EventSystems.StandaloneInputModule: Process() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:280)
    UnityEngine.EventSystems.EventSystem:Update() (at Library\PackageCache\com.unity.ugui@1.0.0\Runtime\EventSystem\EventSystem.cs:514)


    I'm on editor 2021.3.24f1, and if it helps, here are all of my installed packages:
    upload_2023-5-6_15-14-57.png

    I've tried a handful of fixes already. I tried downgrading NGO to 1.3.4, the recommended version, but the same issue remained, so I returned to 1.4.0. I also went to the Unity Relay docs and straight up copied their code on integration with NGO to see if it worked, and it resulted in the same errors. I restarted my computer. I hope somebody here has any insights on what the issue could be.
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    That second error directly results from the first one, which basically means that you are trying to start a server/client without first having set up its Relay server data with
    SetRelayServerData
    . Are you correctly calling this method with the information obtained from the Relay allocation? If you are, are you sure that it's being called on the same instance of the transport that will be used by the
    NetworkManager
    you are starting the server/client on?

    Regarding the resulting memory leaks, they are indeed caused by the errors. This should be fixed in the next version of NGO.
     
  3. Exominem

    Exominem

    Joined:
    Nov 11, 2019
    Posts:
    4
    I'm just confused about the first error in general. I'm using
    SetRelayServerData
    before I start a server/client. I know I'm doing everything right, as I copied the code straight from the docs after my code didn't work only to still get the same error. I'm going to try setting up a new project with the bare essentials for NGO and Relay and seeing if I get the same errors.
     
  4. Exominem

    Exominem

    Joined:
    Nov 11, 2019
    Posts:
    4
    Alright, as it turns out I was being dumb, and I still had my old script running that was also attempting to start a host resulting in the errors.