gpt4 book ai didi

c# - 使用 BinaryFormatter 的反序列化产生 OutOfMemoryException

转载 作者:行者123 更新时间:2023-11-30 13:04:49 27 4
gpt4 key购买 nike

我正在尝试让客户端应用程序将“MessageObject”发送到服务器应用程序。

每次我尝试反序列化服务器上​​的 MessageObject 时,我都会收到 OutOfMemoryException。


首先,这是MessageObject类:

[Serializable]
public class MessageObject : ReplicableObject {
public string Command;
public string[] Parameters;

public MessageObject() {

}

public MessageObject(string command, string[] parameters) {
Command = command.ToLower();
Parameters = parameters;
}
}

MessageObject 是 ReplicableObject 的子对象。这是ReplicableObject:

[Serializable]
public abstract class ReplicableObject {
public string UniqueID {
get {
if (uniqueID == "" || uniqueID == null) {
uniqueID = DateTime.Now.Subtract(new DateTime(1990, 1, 1)).TotalMilliseconds.ToString();
uniqueID = uniqueID.Substring(uniqueID.Length / 2) + Engine.Random(1000000, 9999999).ToString();
}
return uniqueID;
}
private set { uniqueID = value; }
}
private string uniqueID;

[NonSerialized]
public bool RequiresReplication = true;

public ReplicableObject() {
uniqueID = DateTime.Now.Subtract(new DateTime(1990, 1, 1)).TotalMilliseconds.ToString();
uniqueID = uniqueID.Substring(uniqueID.Length / 2) + Engine.Random(1000000, 9999999).ToString();
}
}

当客户端准备好向服务器发送 MessageObject 时,这是它使用的代码:

public static void SerializeRO(Stream stream, ReplicableObject ro) {
Formatter.Serialize(stream, ro);
stream.Write(ASCIIEncoding.ASCII.GetBytes("endhtobject"), 0, 11);
}

TerminationString 是:

public static byte[] TerminationString = ASCIIEncoding.ASCII.GetBytes("endhtobject");

一旦服务器接收到数据,就会调用这个方法(这是抛出异常的地方):

public static ReplicableObject CheckByteStringForRO(byte[] byteString) {
int tStringIndex = 0;
for (int i = 0; i < byteString.Length; ++i) {
if (byteString[i] == TerminationString[tStringIndex]) {
++tStringIndex;
if (tStringIndex >= TerminationString.Length) {
MemoryStream ms = new MemoryStream();
ms.Write(byteString, 0, i - 10);
ms.Position = 0;
ReplicableObject ro = (ReplicableObject) Formatter.Deserialize(ms);
ms.Close();
return ro;
}
}
else tStringIndex = 0;
}
return null;
}

上面的大部分方法只是搜索 TerminationString,所以这里是重要的几行:

MemoryStream ms = new MemoryStream();
ms.Write(byteString, 0, i - 10);
ms.Position = 0;
ReplicableObject ro = (ReplicableObject) Formatter.Deserialize(ms);
ms.Close();
return ro;

在以“ReplicableObject ro =”开头的行中,抛出了 OutOfMemoryException。我不明白这是怎么发生的,尤其是考虑到我发送的对象很小。

我应该指出,我是通过网络发送序列化数据的新手,所以我在这方面可能做错了什么。

如果您需要任何说明,请发表评论。 :)

谢谢。


编辑:根据要求对异常进行堆栈跟踪:

mscorlib.dll!System.Runtime.Serialization.Formatters.Binary.BinaryObjectWithMap.Read(System.Runtime.Serialization.Formatters.Binary._BinaryParser input = {System.Runtime.Serialization.Formatters.Binary._BinaryParser}) + 0x4f bytes
mscorlib.dll!System.Runtime.Serialization.Formatters.Binary._BinaryParser.ReadObjectWithMap(System.Runtime.Serialization.Formatters.Binary.BinaryHeaderEnum binaryHeaderEnum) + 0x38 bytes
mscorlib.dll!System.Runtime.Serialization.Formatters.Binary.
_BinaryParser.Run() + 0x304 bytes
mscorlib.dll!System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(System.Runtime.Remoting.Messaging.HeaderHandler handler = null, System.Runtime.Serialization.Formatters.Binary.__BinaryParser serParser, bool fCheck, bool isCrossAppDomain, System.Runtime.Remoting.Messaging.IMethodCallMessage methodCallMessage = null) + 0xaf bytes
mscorlib.dll!System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, bool fCheck, bool isCrossAppDomain, System.Runtime.Remoting.Messaging.IMethodCallMessage methodCallMessage) + 0xcf bytes
mscorlib.dll!System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(System.IO.Stream serializationStream) + 0x10 bytes

HolotypeTwo.dll!HolotypeTwo.Engine.CheckByteStringForRO(byte[] byteString = {byte[8192]}) Line 34 + 0x10 bytes C# HolotypeServer.exe!HolotypeServer.UnauthorisedPlayer.StartListening() Line 27 + 0x8 bytes C# mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes

最佳答案

您是否检查过要反序列化的内容?由于代码中的缺陷,您可能偶尔会在流中获得两个对象。如果“e”字节紧接在标记之前出现,您的 CheckByteStringForRO 方法最终可能会跳过其终止标记。为了正确捕捉这种情况,您的其他条件需要是:

else
{
tStringIndex = 0;

if (byteString[i] == TerminationString[tStringIndex])
{
tStringIndex++;
}
}

此外,为什么不直接创建一个对象集合并对其进行序列化,而不是使用终止标记之类的呢?

关于c# - 使用 BinaryFormatter 的反序列化产生 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7223889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com