From 0ac4c151e08a9735d085c08ca5bcd9b50944d650 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 26 八月 2019 16:41:35 +0800
Subject: [PATCH] memleak

---
 shm.go |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/shm.go b/shm.go
index 727d18c..8dec56e 100644
--- a/shm.go
+++ b/shm.go
@@ -13,6 +13,8 @@
 type SHM struct {
 	rw  *shm.ReadWriteCloser
 	typ td
+
+	recvData []byte
 }
 
 // Send impl interface Diliver
@@ -36,10 +38,13 @@
 		return nil, errors.New("please open shm consumer first")
 	}
 
-	data := make([]byte, maxRecvSize)
-	n, err := s.rw.Read(data)
+	if s.recvData == nil {
+		s.recvData = make([]byte, maxRecvSize)
+	}
+	n, err := s.rw.Read(s.recvData)
 	if err == nil || err == io.EOF {
-		data = data[:n:n]
+		data := make([]byte, n)
+		copy(data, s.recvData)
 		return data, nil
 	}
 
@@ -88,8 +93,9 @@
 	if err == nil {
 		fmt.Println(rw.Name())
 		return &SHM{
-			rw,
-			agent,
+			rw:       rw,
+			typ:      agent,
+			recvData: nil,
 		}, nil
 	}
 
@@ -104,8 +110,9 @@
 	rw, err := shm.OpenSimplex(url)
 	if err == nil {
 		return &SHM{
-			rw,
-			coactee,
+			rw:       rw,
+			typ:      coactee,
+			recvData: nil,
 		}, nil
 	}
 	return nil, err

--
Gitblit v1.8.0