fix: fixes crash when fertilizing crops #4932
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
Required
main
so it won't cause conflict when updatingmain
branch later.Purpose of change
Fixes #4446. Verify by attempting to fertilize any planted crop, which would previously cause a seg fault and crash the game.
Describe the solution
I believe this was caused by the order of the code in iexamine::fertilize_plant. I'm skipping some of the steps, but the relevant ones were in this order before: get a pointer to the seed item being fertilized -> spawn fertilizer item on that tile (so it can only be fertilized once) -> use seed pointer to format a game message with the proper plant name. I think that spawning the fertilizer would invalidate the seed pointer and cause the seg fault when trying to get its name. I just changed the order so that the fertilizer is spawned before we use the iterator to get the seed pointer.
Describe alternatives you've considered
Still new to C++ so this was the only solution I could come up with. I'm more than open to others but this felt like the safest way to fix the issue without breaking something else.
Testing
Fertilized several crop types (wheat, tomatoes, beans) with both commercial and liquid fertilizer. No crash. Was still only able to fertilize each tile once.
Additional context