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

c#对象提前销毁后,lua层userdata引用不能正常回收引发的问题 #1183

Open
yiyucyp opened this issue Jan 22, 2025 · 1 comment

Comments

@yiyucyp
Copy link

yiyucyp commented Jan 22, 2025

当c#的对象如果提前销毁后,xlua本身有机制,会在Tick中发现,并处理

  1. 会把ObjectPool中的Slot找到并归还,此时会一同归还index,也就是lua层userdata中保存的数值。
  2. 相应的reverseMap也做了移除操作。
  3. 此时lua层的userdata引用还在,所以还会尝试通过保存的Index来访问C#对象,第一时间会触发null异常,因为ObjectPool中的这个Index未使用。
  4. 但是当其他的lua变量再次使用c#对象时,这个index就会被重复使用,而此时就会出现问题
  5. 旧的userdata中的索引与新的userdata中索引是同一个,当C#对象的类型不同时会触发异常,类型相同时,操作就不可预知
  6. 由于ObjectPool的索引操作是类似栈的后进先出,所以很容易出现复用。
    请教下,这个问题如何解决,是不是严格要求不能出现C#对象提前释放的现象,也就是Lua引用必须先于C#对象释放设置为nil
@yiyucyp
Copy link
Author

yiyucyp commented Jan 22, 2025

前面复用的逻辑分析发现是错的,Tick中发现C#对象销毁后,只会把此Slot中的obj调为null,slot并不会归还给pool。所以userdata中索引错乱,应该是有其他什么原因引起的。

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

1 participant