- Home /
what would cause a packet to be "disposed of" while still inside a Using statement?
I am trying to do some networking in Unity, but I am catching a SendDataSystem.ObjectDisposedException: Cannot access a disposed object. Exception while executing code inside of my using statement.
Images of Code:
-ClientSend 1
public static void SendItem(int itemLVL, int toStationID)
{
Debug.Log("Sending Item on channel : " + (int)ClientPackets.item);
using (sPacket packet = new sPacket((int)ClientPackets.item))
{
packet.Write(itemLVL);
packet.Write(toStationID);
SendTCPData(packet);
}
}
-ClientSend 2
private static void SendTCPData(sPacket packet)
{
packet.WriteLength();
Client.instance._tcp.SendData(packet);
}
-Client.tcp
public void SendData(sPacket packet)
{
try
{
if (_socket != null)
{
_stream.BeginWrite(packet.ToArray(), 0, packet.Length(), null, null);
}
}
catch (Exception e)
{
Debug.LogError("Error in SendData" + e);
}
}
what would cause a packet to be "disposed of" by the time SendTCPData(packet)
calls Client.instance._tcp.SendData(packet)
then SendData(packet)
catches the exception ObjectDisposedException: Cannot access a disposed object
? I understand that the using(packet)
syntax will auto call Dispose when it ends the block, however I dont understand why the stack is messing this up somehow on its calls down?
Shouldn't it go : ClientSend.SendItem() open the using block--> ClientSend.SendTCPData() --> Client._tcp.SendData() uses data, then stack goes upward and the using block closes?
Note: The tcp connection was previously opened when connecting
Answer by Havie · Nov 25, 2020 at 03:30 AM
To answer my own question,
I believe what was happening was while I was writing this post I removed the UIManager.instance.DebugLogs() which were printing text to a self-made debug window since this was being run on tablets and a desktop. However, some of these calls are Asynchronous so I was getting errors that weren't being thrown even to the desktop console in unity. It wasn't till I surrounded my custom debugger with try-catch that I started receiving
get_isActiveAndEnabled can only be called from the main thread.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity tcp_client only working on localhost 1 Answer
Trouble Connecting Unity with Labview 0 Answers