Skip to content

Commit

Permalink
Lua: Use atomic writes for pointer fields to match engine behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 8, 2025
1 parent 0613741 commit 583192f
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/mods/bindings/Sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,13 +1014,22 @@ void set_data(void* data, ::sdk::RETypeDefinition* data_type, sol::object& value
}

REManagedObject** field = (REManagedObject**) data;
if (field != nullptr && *field != nullptr) {
utility::re_managed_object::release(*field);
}
if (new_data != nullptr) {
utility::re_managed_object::add_ref(new_data);
if (field != nullptr && *field != new_data) {
if (new_data != nullptr) {
utility::re_managed_object::add_ref(new_data);
}

// Use a thread-safe atomic exchange. This is what Capcom does for all field assignments.
auto old = *field;
while (_InterlockedCompareExchangePointer((PVOID*)field, new_data, old) != old) {
old = *field;
}

if (old != nullptr) {
utility::re_managed_object::release(old);
}
}
*(REManagedObject**) data = new_data;

return;
}
}
Expand Down

0 comments on commit 583192f

Please sign in to comment.