This is a bit strange of a concept to me. At least in how the OP originally proposed it. Leafs are tight up against the cap and only have about 190k in room. Basically nothing. Sharks have around 2.2 million. So some but not much. Dubnyk is either a slight upgrade or slight downgrade on Anderson depending on how you look at it. But either way this is a largely lateral move. Both contracts expire at the end of the year so no lasting cap problems.
The reason Toronto should do this is with the out look of maybe a change of scenery helps Dubnyk after spending a season being hung out to dry by San Jose's defense. To any Toronto fans who don't watch Sharks games, this happens multiple times on a nightly basis. Just getting away from that should help. But more importantly the Leafs would get cap space to make another move to help their roster. In this scenario it's not the leafs retaining but rather the Sharks. Sharks do this to get an asset and potentially have a goalie around to extend to help with future plans at the position.
If the Leafs big move is in fact upgrading on Anderson then the Sharks could potentially still be involved as a 3rd team to help retain on cap in exchange for a mid to late round pick. Not sure who Leafs fans would want in that scenario or if it is even necessary for whatever team they are trading with. For the Sharks the cap hit probably couldn't extend beyond next season in any retention scenario.