Added the keep flag, found the snippet in the Code Repository, but can't seem to make it work propely.
First time you get killed the item stays in the inventory like it should. However after that any time you get killed this happens: [*****] BUG: obj_from_char: null ch. and the item is gone, replaced by your guts or leg or some other bodypart.
You don't give much to go on here, so I can only make a guess based on having some idea of how the code you are working with probably works. I would look to make sure you are NOT extracting the object. In some merc derived code, the player is extracted, but it is not a real extraction as in the char_data being freed, but in these codebases the extract_char function extracts any objects on the char. In a normal death the objects would have already been transfered to the corpse, so the char is empty. But in any case, I would think this is from extracting an object more then once or messing up in the obj_from_whatever and obj_to_whatever (like putting an object someplace without taking it from it's starting place, or the like).