Hilchos Lashon Hara chapter 4, section 10

Despite it is forbidden to talk about his shameful behavior, it is correct for this observer to tell his (own) son or his students and warn them not to associate with this person so that they will not learn to emulate this person lifestyle. The essential reason why Lashon Hara is forbidden by the Torah is because the speaker’s intent is to humiliate his fellow Jew and rejoice in his disgrace. But if the speaker’s intention is to protect his friend so that he will not learn to copy this person lifestyle, then it is obviously permissible to disclose this person actions. However in this kind of circumstance it is a mitzvah for the speaker to explain the reason why the discloser is being made, in order that the listener should understand the reason for the comment and not think that this type of speech is ordinary permissible. Moreover, the listener should not think the speaker is contradicting himself since he had said prior that Lashon Hara is forbidden even if it is truth.