In addition to code sharing and revenue sharing, carriers in the same alliance share FF benefits. For example, a UA 1K flying LH will likely get all the FF benefits he is used to, like priority check in, lounge access in the LH lounge, etc. If one never flys UA, why would he credit miles to the MP program? If he did, UA loses nothing. They never have to provide those benefits for someone who never flys UA. In addition, many programs give better benefits to their own FFs. For example BA gives only its own elites access to the Concorde room, not other oneworld elites (not *A, I know, but I am more familiar with OW being an AA flyer), and SQ releases very limited F award availability to its *A partners. Thus, I doubt airlines lose money in the way you describe. And also, you generally cannot achieve elite status on an airline without flying on it at all. For example, AA requires at least 4 segments on AA metal to achieve any sort of elite status.
Likely, both airlines get revenue from their code shares, and the global network the alliance creates generates more revenue for all airlines in the alliance.