pDoc not deleted if any created element is null. Can you create RAII wrapper to ensure that XMLDocument will be deleted? Probably std::auto_ptr is good choice.