An officer can look up a person's record for pretty much any reason. The most common situation is when an officer gets a person's license during a traffic stop or even when the officer is driving behind a person. The officer can look up a person's record to check if there are any warrants out for the person and in the process they learn the history. Generally there isn't anything inherently illegal about the officer sharing factual and publicly available information to others although it wouldn't necessarily be very professional. If this is happening to you then you can lodge a complaint to the police department about the officer involved.