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

Fix TileMapEditorPlugin crash by storing tilemap ID instead of pointer #80610

Merged

Conversation

lawnjelly
Copy link
Member

Store the tilemap ObjectID instead of raw pointer, and check it is valid before access.

Fixes #80609
Alternative to #80537

Notes

  • Stores ObjectID for all accesses rather than raw pointer
  • Ref counting would have also maybe worked for this, but I don't think Nodes are ref counted.

@lawnjelly lawnjelly added this to the 4.x milestone Aug 14, 2023
@akien-mga akien-mga requested a review from KoBeWi August 14, 2023 10:30
@akien-mga akien-mga modified the milestones: 4.x, 4.2 Aug 14, 2023
@akien-mga
Copy link
Member

Is this relevant for 4.1.x too, or fixing a regression from 4.2-specific changes?

@lawnjelly
Copy link
Member Author

lawnjelly commented Aug 14, 2023

Amusingly, it looks like 4.1 already has a similar fix already applied. Maybe it got broken doing 4.2, or someone else noticed the same problem there in 4.1. 😀

Yeah @groud I think originally did this with ObjectID (2 years ago) and perhaps @KoBeWi added the raw pointer (in #74717 ? ) which caused the regression.

@lawnjelly
Copy link
Member Author

Failed the GodotTest CI, not sure why, am rerunning in case spurious, it's kind of hard to pin down where it is failing.

If it is genuine maybe there's an access of ObjectDB outside the lifetime or something. 🤷‍♂️ Will look if it fails again.

@KoBeWi
Copy link
Member

KoBeWi commented Aug 14, 2023

I can't reproduce the crash, but I'm getting an error that should be crashing 🤔
image

EDIT:
This PR fixes it though.

@lawnjelly
Copy link
Member Author

lawnjelly commented Aug 14, 2023

I can't reproduce the crash, but I'm getting an error that should be crashing thinking

Both of these are consistent with TileMap being in the process of being destroyed (possibly at the same time on another thread?) before TileMapEditorPlugin::edit() is called with NULL to remove the reference.

The interesting bit is why the CI is failing (assuming not a false flag, I've run it twice), possibly memory corruption / double free going on. I'm not familiar enough with ObjectDB to know offhand if there's an order of destruction problem that could cause this, and I'm not really familiar with running the GodotTest locally.

I'm happy for you @KoBeWi to investigate, or use #80537 which is simpler and doesn't seem to cause this (at least temporarily), or some other solution.

Store the tilemap ObjectID instead of raw pointer, and check it is valid before access.
@lawnjelly lawnjelly force-pushed the fix_tilemap_editor_plugin_crash2 branch from d23a5b5 to 356fc72 Compare August 14, 2023 11:57
@KoBeWi
Copy link
Member

KoBeWi commented Aug 14, 2023

Ah I missed that this is a separate PR from #80537. I though you just applied my requested changes 🙃
This one is more complex, but IMO more correct.

@lawnjelly
Copy link
Member Author

Also just to note, if TileMapEditorPlugin::edit() is being called on a separate thread to TileMap destructor, then this whole sequence would need some thread protection. I'm assuming here both are on the same thread (as is I think the original code).

@KoBeWi
Copy link
Member

KoBeWi commented Aug 14, 2023

Editor works on single thread, only specific tasks (like importing or baking) are multithreaded.

@lawnjelly
Copy link
Member Author

I can't reproduce the crash, but I'm getting an error that should be crashing thinking

Actually, thinking about it, this could come down to differences in our debuggers. Maybe mine is catching the access to deleted object early, and yours is running the function on the remnants of the deleted object.

To check the lifetime was the problem when I wrote this, I put a bool in the TileMap for whether it was active, and set it to false in the destructor. The data still existed in the object remnants, but the destructor had been called.

@lawnjelly
Copy link
Member Author

Sanitizer build has now passed, and the only change was removing that space.
🤷‍♂️

@akien-mga akien-mga merged commit c78be12 into godotengine:master Aug 14, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

@lawnjelly lawnjelly deleted the fix_tilemap_editor_plugin_crash2 branch August 14, 2023 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TileMapEditorPlugin crash when closing tab
3 participants