Rocher0724 / socket.io-unity

MIT License
107 stars 19 forks source link

memory leaks close connection when receiving message #1

Closed arthurmougin closed 4 years ago

arthurmougin commented 4 years ago

Hello!

Using your package in Unity 2019.3.0f3 en .net v4 The communication close after a flood of warnings :

Internal: deleting an allocation that is older than its permitted lifetime of 4 frames (age = 5) Unity.Collections.LowLevel.Unsafe.UnsafeUtility:Free(Void*, Allocator) Unity.Entities.EntityCommandBuffer:Dispose() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/EntityCommandBuffer.cs:927) Unity.Entities.EntityCommandBufferSystem:FlushPendingBuffers(Boolean) (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/EntityCommandBufferSystem.cs:249) Unity.Entities.EntityCommandBufferSystem:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/EntityCommandBufferSystem.cs:188) Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107) Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:182) Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:169) Unity.NetCode.ServerSimulationSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.netcode@0.1.0-preview.6/Runtime/ClientServerWorld/ServerSimulationSystemGroup.cs:72) Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107) Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:182) Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:169) Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107) Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:182) Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystemGroup.cs:169) Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ComponentSystem.cs:107) Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.6.0-preview.24/Unity.Entities/ScriptBehaviourUpdateOrder.cs:152)

and

Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak

the client code is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Socket.Quobject.SocketIoClientDotNet.Client;

public class SocketIoComponent : MonoBehaviour
{

    public string serverURL = "http://localhost:3000";
    protected QSocket socket = null;

    // Start is called before the first frame update
    void Start()
    {

        DoOpen();

    }

    //fonction unity appelée à la destruction de l'object
    void Destroy()
    {
        DoClose();
    }

    //fonction appelé à la fin du programme
    void OnApplicationQuit()
    {
        DoClose();
    }

    void DoOpen()
    {

        if (socket == null)
        {
            socket = IO.Socket(serverURL);
            socket.On("connect", () => {
                Debug.Log("Connecting");
                SendMSG("blablabla");
            });

            socket.On("msg", (data) => {
                Debug.Log(data.ToString());
            });

            socket.On("disconnect", (data) => {
                Debug.Log(data.ToString());
                DoClose();
            });
        }
    }

    void DoClose()
    {
        Debug.Log("Closing");
        if (socket != null)
        {
            socket.Disconnect();
            socket = null;
        }
    }

    void SendMSG(string str)
    {
        if (socket != null)
        {
            Debug.Log("Emitting " + str);
            socket.Emit("msg", str);
        }
    }
}

and the server code is

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', (socket)=>{
  console.log('a user connected');
  socket.on('msg', function(msg){
    console.log('message : ' + msg);
    io.emit('msg', msg);
  });
  socket.on('disconnect',(reason)=>{
      console.log(JSON.stringify(reason));
  })
});

http.listen(3000, ()=>{
  console.log('listening on *:3000');
});

The nodejs log node-screen-cap

the unity log unity-screen-cap

Do you know where it comes from? Thanks in advance !

Edit : Thanks btw for your version, the original one would not work at all... :/

Rocher0724 commented 4 years ago

first issue!

I'm so sorry. I've been so busy that I didn't care about this repo. I'll test it out and let you know as soon as possible.

arthurmougin commented 4 years ago

Hello again, I have updated unity to the 2019.3.7f1 version and the problem is solved. No more memory leaks! I have from time to time a timeout in the connection but auto-reconnecting is the answer :)

Thanks for your time!