Skip to content

Commit c63ddc7

Browse files
committed
插件发送消息可能泄露 #998
kbengine/kbengine#998
1 parent 3663422 commit c63ddc7

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Bundle.cs

+15-12
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,20 @@ public Bundle()
2525

2626
public void clear()
2727
{
28-
stream = MemoryStream.createObject();
29-
streamList = new List<MemoryStream>();
28+
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
29+
for (int i = 0; i < streamList.Count; ++i)
30+
{
31+
if (stream != streamList[i])
32+
streamList[i].reclaimObject();
33+
}
34+
35+
streamList.Clear();
36+
37+
if(stream != null)
38+
stream.clear();
39+
else
40+
stream = MemoryStream.createObject();
41+
3042
numMessage = 0;
3143
messageLength = 0;
3244
msgtype = null;
@@ -110,21 +122,12 @@ public void send(NetworkInterface networkInterface)
110122
Dbg.ERROR_MSG("Bundle::send: networkInterface invalid!");
111123
}
112124

113-
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
114-
for (int i = 0; i < streamList.Count; ++i)
115-
{
116-
streamList[i].reclaimObject();
117-
}
118-
119-
streamList.Clear();
120-
stream.clear();
121-
122125
// 我们认为,发送完成,就视为这个bundle不再使用了,
123126
// 所以我们会把它放回对象池,以减少垃圾回收带来的消耗,
124127
// 如果需要继续使用,应该重新Bundle.createObject(),
125128
// 如果外面不重新createObject()而直接使用,就可能会出现莫名的问题,
126129
// 仅以此备注,警示使用者。
127-
Bundle.reclaimObject(this);
130+
reclaimObject();
128131
}
129132

130133
public void checkStream(int v)

0 commit comments

Comments
 (0)