Regarding the duplication issue, instead of removing this bug, you could try to create a fix for it.
Asm said that this is because the hero is stored in the save file (and in memory) in two places – in the list of heroes and in the list of map objects. The duplication occurs because the coordinates in one place differ from the coordinates in the other, causing one of the copies to "stick."
Consequently, it can be fixed by removing the copy that remains on the map. It's a bit more complicated with castles if the hero duplicates while inside (this often happens with the AI).
In other words, instead of eliminating the cause of the bug, you can create a fix that is applied when the bug occurs.
The situation with castles is also simple.
In the castle structure, there are 2 bytes – one for the garrison hero and one for the visiting hero. If a hero is not present, the corresponding number is -1.
Therefore, if the garrison hero is the same as the visiting hero, one of them needs to be removed.
The visiting hero can leave the castle, which is recognized by the coordinates of the moving hero (which do not match the coordinates of the castle), in which case the garrison hero needs to be removed.
Sometimes both heroes manage to leave the castle, in which case there are two identical heroes in the player's structure (where the 8 moving heroes are stored). This can also be fixed by removing one of the two identical heroes.
And also, such a hero can end up in the tavern of an opponent or the AI, in which case two players will control the same hero. No one knows what bugs this could lead to.
And most importantly, the above-mentioned bugs are not the only ones!
There is also the issue of the castle turning gray, and it also happens in different ways. Sometimes the game crashes when you hover the mouse over the castle (even without pressing any keys). Once, during my turn, the castle changed the colors of all players in turn (from red to purple), and we were playing without the AI.
So, the hero's shadow is far from the only problem...
PS. I have a save file (from offline mode) where the hero's shadow appears when you click "view opponent's turn." I will provide it upon request.