lua table.remove引发的偶现bug

故事背景,两只宠物育种的时候要从背包删除,放入育种栏。育种完成再放回背包。现在育种完成背包多了一只。调试发现是删除的时候少删除了一只。

Lua 复制代码
if source_list_a == take_list then
	table.remove(take_list, index_a)
else
	table.remove(ware_list, index_a)
end
if source_list_b == take_list then
	table.remove(take_list, index_b)
else
	table.remove(ware_list, index_b)
end

如果a在b的前面,那么b remove会失败,remove后b的index_b其实已经改变了.

其实这个bug是ai写的,下面的代码也是ai改的

Lua 复制代码
-- 先移除索引较大的宠物,再移除索引较小的,避免索引变化问题
local remove_indices = {}
if source_list_a == take_list then
    table.insert(remove_indices, {list = take_list, index = index_a})
else
    table.insert(remove_indices, {list = ware_list, index = index_a})
end
if source_list_b == take_list then
    table.insert(remove_indices, {list = take_list, index = index_b})
else
    table.insert(remove_indices, {list = ware_list, index = index_b})
end
-- 按索引从大到小排序,先移除索引大的
table.sort(remove_indices, function(a, b)
    return a.index > b.index
end)
-- 按排序后的顺序移除
for _, remove_info in ipairs(remove_indices) do
    table.remove(remove_info.list, remove_info.index)
end
相关推荐
最贪吃的虎1 天前
Redis其实并不是线程安全的
java·开发语言·数据库·redis·后端·缓存·lua
秃头小饼干1 天前
技术文章大纲:Bug悬案侦破大会
bug
hui函数2 天前
Python系列Bug修复PyCharm控制台pip install报错:如何解决 pip install 网络报错 企业网关拦截 User-Agent 问题
python·pycharm·bug
hui函数2 天前
如何解决 pip install 代理报错 SOCKS5 握手失败 ReadTimeoutError 问题
bug·pip
f***24112 天前
Bug悬案:程序员破案实录
bug
_200_2 天前
Lua 流程控制
开发语言·junit·lua
Kang.Charles2 天前
Lua UI系统框架逻辑详解
ui·lua
oMcLin2 天前
如何在 Rocky Linux 8.6 上配置并调优 Nginx 与 Lua 脚本,提升 API 网关的性能与并发处理能力
linux·nginx·lua
一条咸鱼_SaltyFish3 天前
[Day10] contract-management初期开发避坑指南:合同模块 DDD 架构规划的教训与调整
开发语言·经验分享·微服务·架构·bug·开源软件·ai编程
雒珣3 天前
qt界面和图片疯狂变大的bug问题
开发语言·qt·bug