One thing that does not seem to have been emphasized in the other answers so far is that it matters quite a lot who you share your ideas with. There are some people that should be avoided.
In graduate school I solved a problem that had been circulating in the department for a while. Before I had had a chance to write it up formally, someone (more senior than myself) who had tried unsuccessfully to solve it asked me to explain my solution to him. As I started to explain my solution, he would repeatedly interrupt and say, "Oh I see now...this is what you do." Then he would start talking and writing on the blackboard. When he got stuck (because he didn't really see how to solve it), he would then stop and invite me to continue. The scenario would repeat. He would also criticize my presentation. The whole time, he acted as if he didn't believe that I had solved the problem, and seemed to be trying to get me to give him my ideas while behaving in a way that would allow him to say afterwards that I had had some good ideas but had not really solved the problem and that he was the one who had really solved it. I don't know if he was doing this intentionally or whether this was just his personality, but needless to say, it was a very unpleasant experience and I avoided sharing my ideas with him from then on.
While in general I am a fan of sharing ideas, I think one should be aware that there are people out there who (consciously or unconsciously) do steal ideas. They are rare (the above person is the only one that I explicitly try to avoid sharing ideas with) but they do exist.