If its looping, run it through a debugger. If you haven't got one, or think its not practicle to use one, try putting lots of printf("checkpoint 1"); type statements in the code to help you pin-point the problem area.