Skip to content

Commit

Permalink
Fix use-after-free at shutdown, sync API files
Browse files Browse the repository at this point in the history
  • Loading branch information
mihe committed Oct 11, 2022
1 parent 779eaba commit daadd05
Show file tree
Hide file tree
Showing 3 changed files with 1,650 additions and 524 deletions.
15 changes: 15 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,8 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
if is_singleton:
result.append(f"\tstatic {class_name} *get_singleton();")
result.append("")
result.append(f"\t~{class_name}();")
result.append("")

if "methods" in class_api:
for method in class_api["methods"]:
Expand Down Expand Up @@ -1291,6 +1293,8 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
result.append("")

if is_singleton:
result.append(f"static bool {class_name}_singleton_destroyed = false;")
result.append("")
result.append(f"{class_name} *{class_name}::get_singleton() {{")
result.append(
f'\tstatic GDNativeObjectPtr singleton_obj = internal::gdn_interface->global_get_singleton("{class_name}");'
Expand All @@ -1301,9 +1305,20 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
result.append(
f"\tstatic {class_name} *singleton = reinterpret_cast<{class_name} *>(internal::gdn_interface->object_get_instance_binding(singleton_obj, internal::token, &{class_name}::___binding_callbacks));"
)
result.append(f"\tstatic struct {class_name}_BindingsCleanup {{")
result.append(f"\t\t~{class_name}_BindingsCleanup() {{")
result.append(f'\t\t\tif (!{class_name}_singleton_destroyed) {{')
result.append("\t\t\t\tinternal::gdn_interface->object_clear_instance_binding(singleton_obj, internal::token);")
result.append("\t\t\t}")
result.append("\t\t}")
result.append("\t} bindings_cleanup;")
result.append("\treturn singleton;")
result.append("}")
result.append("")
result.append(f"{class_name}::~{class_name}() {{")
result.append(f"\t{class_name}_singleton_destroyed = true;")
result.append("}")
result.append("")

if "methods" in class_api:
for method in class_api["methods"]:
Expand Down
Loading

0 comments on commit daadd05

Please sign in to comment.