The court may appoint a guardian ad litem to any infant or defendant of unsound mind, and allow him suitable compensation payable out of the estate of such party, but the appointment shall not be made except when the court shall consider it necessary for the protection of the interest of such defendant; and a decree or judgment of any court shall not be void or erroneous because of the failure to have a guardian ad litem.