More info. It is actually only when using some Qt function that accesses the OS file system GUI/dialog (hmm, probably any OS GUI/dialog for that matter) that the Highgui library causes the program to hang when closing. I tried using QTreeView and QDirModel to work around the QFileDialog::getOpenFileName() function, but QDirModel also caused the same problem. But then I tried QTreeWidget and QDir to create the items and it worked. No more hang up when closing.
Bookmarks