Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone函数切片应该使用深拷贝 #293

Open
trash-boy opened this issue Dec 16, 2023 · 5 comments
Open

clone函数切片应该使用深拷贝 #293

trash-boy opened this issue Dec 16, 2023 · 5 comments

Comments

@trash-boy
Copy link
Contributor

func (h *HeartbeatChecker) Clone() ziface.IHeartbeatChecker {

	heartbeat := &HeartbeatChecker{
		interval:         h.interval,
		quitChan:         make(chan bool),
		beatFunc:         h.beatFunc,
		makeMsg:          h.makeMsg,
		onRemoteNotAlive: h.onRemoteNotAlive,
		msgID:            h.msgID,
		router:           h.router,
		routerSlices:     h.routerSlices,
		conn:             nil, // The bound connection needs to be reassigned
	}

	return heartbeat
}

其中routerSlices是一个切片,他应该使用深拷贝,否则修改原始hb会修改新的hb,例如

func  HeatBeatDefaultHandle111(req ziface.IRequest) {
	zlog.Ins().InfoF("Recv Heartbeat from %s, MsgID = %+v, Data = %s",
		req.GetConnection().RemoteAddr(), req.GetMsgID(), string(req.GetData()))
}
func TestHeartbeatChecker_Clone(t *testing.T) {
	hb := NewHeartbeatChecker(1)
	//t.Log(len(hb.RouterSlices()), cap(hb.RouterSlices()))
	new_hb := hb.Clone()

	new_hb.BindRouterSlices(123,HeatBeatDefaultHandle)
	new_hb.BindRouterSlices(123,HeatBeatDefaultHandle)
	//t.Log(len(new_hb.RouterSlices()), cap(new_hb.RouterSlices()), new_hb.RouterSlices())

	new_new_hb := new_hb.Clone()
	//t.Log(len(new_new_hb.RouterSlices()), cap(new_new_hb.RouterSlices()),new_new_hb.RouterSlices())
	new_new_hb.BindRouterSlices(123,HeatBeatDefaultHandle)
	t.Log("new_new_hb",len(new_new_hb.RouterSlices()), cap(new_new_hb.RouterSlices()),new_new_hb.RouterSlices())
	t.Log("new_hb",len(new_hb.RouterSlices()), cap(new_hb.RouterSlices()),new_hb.RouterSlices())

	new_hb.BindRouterSlices(123,HeatBeatDefaultHandle111)
	t.Log("new_new_hb",len(new_new_hb.RouterSlices()), cap(new_new_hb.RouterSlices()),new_new_hb.RouterSlices())
	t.Log("new_hb",len(new_hb.RouterSlices()), cap(new_hb.RouterSlices()),new_hb.RouterSlices())
}

进行测试会发现,修改原始new_hb,new_new_hb会修改

    heartbeat_test.go:24: new_new_hb 4 4 [0x1022c2ed0 0x1022c2ed0 0x1022c2ed0 0x1022c2ed0]
    heartbeat_test.go:25: new_hb 3 4 [0x1022c2ed0 0x1022c2ed0 0x1022c2ed0]
    heartbeat_test.go:28: new_new_hb 4 4 [0x1022c2ed0 0x1022c2ed0 0x1022c2ed0 0x1022cc0e0]
    heartbeat_test.go:29: new_hb 4 4 [0x1022c2ed0 0x1022c2ed0 0x1022c2ed0 0x1022cc0e0]
@aceld
Copy link
Owner

aceld commented Dec 18, 2023

@trash-boy 是否指的 routerSlices 有浅拷贝问题?

@trash-boy
Copy link
Contributor Author

我觉得是有问题的,应该使用深拷贝

aceld added a commit that referenced this issue Dec 18, 2023
@aceld
Copy link
Owner

aceld commented Dec 18, 2023

@trash-boy #293 这里有个pr,可否review下

@trash-boy
Copy link
Contributor Author

@trash-boy #293 这里有个pr,可否review下

好的 我会看看的

@aceld
Copy link
Owner

aceld commented Dec 18, 2023

#294

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants