High resolution electron microscopy has been applied to study the structure of epitaxial GaN layers grown by MOVPE on SiC and sapphire substrates. Defects in GaN were systematically studied for undoped, and Si- and Mg-doped samples. For both substrates, the Si-doping was found to decrease the dislocation density at the layer surface, while Mg-doping increased it. The density of nanopipes increased with both types of doping. Cracking of GaN layers was observed for SiC substrates. Crack formation was not detected in layers grown on sapphire. Mechanisms of defect generation are discussed in relation to the initial growth stages, the effect of doping, and the type of substrate.