fix possible crash
fix #7713
fix #35876
fix #39121, fix #20783
fix #40106