[C/C++ Only] Resolving the exception thrown by PDFNet.Terminate() or Doc destructor.

Q: I am using the following C/C++ code to embed a font to an existing
PDF. The exception is thrown in PDFNet.Terminate() or SDFDoc
destructor. Why is wrong with my code?

#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/Font.h>

static void main () {
  pdftron::PDFNet::Initialize ();
  pdftron::SDF::SDFDoc doc ("foo.pdf");
  pdftron::PDF::Font font = pdftron::PDF::Font::CreateTrueTypeFont
(doc, "font.ttf", true);
  // The next line triggers an access violation in PDFNet::Terminate
().
  font.GetUnitsPerEm ();
  doc.Save("out.pdf", pdftron::SDF::SDFDoc::e_incremental, 0, "");
  pdftron::PDFNet::Terminate ();
}
-----
A: The problem is that PDFNet.Terminate() should be called as a last
call to PDFNet (similarly, PDFNet.Initialize() should be called before
any other call to PDFNet). In your code document and font destructors
are being called 'after' the call to PDFNet.Terminate() and as a
result terminate throws an exception.

To fix the problem, make sure that PDFDoc and SDFDoc are destroyed
after PDFNet.Terminate() using the scoping rules:

static void main () {
  PDFNet::Initialize();
  PDFNet::SetResourcesPath(...);
  {
      SDFDoc doc (pdf_path);
      ...
  }
  PDFNet::Terminate ();
}