If it appear on the trial of any habeas corpus that the relator is held by virtue of proceedings against him for crime which are invalid, the judge shall not discharge the relator because thereof, but shall investigate the facts; and if it be found that he ought to be held for any crime alleged against him, the judge shall not discharge him, but shall commit him or require bail, according to the nature of the case.