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.
Summary
SUMMARY: Bugfixes "fixes occasional debugmsg and segfaults caused by using items in inventory"
Purpose of change
Using items via the item
a
ction menu would occasionally cause a debugmsg, sometimes followed by a segfault, and in both cases the used item vanished and the activity failed.I tracked this problem down to
avatar_action::use_item
where the debugmsg is thrown, and further to some shenanigans initem_location::obtain()
foritem_in_container
variants.The theory is that during obtain(), the item gets moved from its original location and placed in a player's inventory; when it's being obtained from a container, that container is sometimes a pocket on the player, and sometimes item gets placed back in the same pocket. When
remove_item()
is subsequently called the item then gets deleted, and the returned item_location has a null target item.Describe the solution
Check if the item's container is already held by the player, and if so charge the move cost to obtain the item but do not move it around, since it's already in the player's inventory.
Describe alternatives you've considered
I can't think of a more elegant way to fix this. One option was further hacks and special case checks in
use_item
around the debug message. That worked but was ugly (also, this problem may crop up elsewhere so not a total solution).Testing
Additional context
This one is really difficult to reproduce consistently. Even sharing my save file with KorG where I'd set up the circumstance, he could not reproduce it.
So I'm not sure how anybody else can verify this works unless they can also get the bug to happen for them. I'm also not 100% sure my diagnosis describes what's going on adequately.
I do believe this is a valuable safety/sanity check regardless of whether it solves a problem immediately for anyone other than myself.
Side effect
This fixes the problem brought up in the now-deleted TODO in the diff: the player will be charged an appropriate amount of moves for obtaining the item regardless of where it comes from, so the refund is no longer sensible or necessary. If the costs need to be adjusted henceforth it should be done in the appropriate
obtain_cost()
function rather than with a hack inuse_item
.