Sometimes the only way to find a mate and avoid competition with other males, in some insect species, is to wait around a pupa and court as soon as the female emerges. At least one butterfly species can’t wait that long (and if you go to the photo gallery, you’ll see there’s quite a strong male competition even at that stage), and mating may occur before the female emerged. You can find photographic evidence for this pupal mating there.
There’s even a short video about competition for pupae, enjoy:








