Cleanup: use ScopeGuard.
authorCarl Hetherington <cth@carlh.net>
Tue, 3 Jan 2023 16:28:53 +0000 (17:28 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 3 Jan 2023 16:28:53 +0000 (17:28 +0100)
src/tools/dcpomatic_kdm.cc

index 5a7b4e6de9c927f7c6527492300d0b6098b0b82c..0d6ecbab011d28b574360d93d53db7eba8097e24 100644 (file)
@@ -46,6 +46,7 @@
 #include "lib/file_log.h"
 #include "lib/job_manager.h"
 #include "lib/kdm_with_metadata.h"
+#include "lib/scope_guard.h"
 #include "lib/screen.h"
 #include "lib/send_kdm_email_job.h"
 #include <dcp/encrypted_kdm.h>
@@ -497,47 +498,50 @@ private:
                        wxFD_MULTIPLE
                        );
 
-               if (d->ShowModal() == wxID_OK) {
-                       auto chain = Config::instance()->decryption_chain();
-                       DCPOMATIC_ASSERT (chain->key());
-
-                       wxArrayString paths;
-                       d->GetPaths(paths);
-                       for (unsigned int i = 0; i < paths.GetCount(); ++i) {
-                               try {
-                                       dcp::EncryptedKDM ekdm(dcp::file_to_string(wx_to_std(paths[i]), MAX_KDM_SIZE));
-                                       /* Decrypt the DKDM to make sure that we can */
-                                       dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
-
-                                       auto new_dkdm = make_shared<DKDM>(ekdm);
-                                       auto group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm());
-                                       if (!group) {
-                                               group = Config::instance()->dkdms ();
-                                       }
-                                       add_dkdm(new_dkdm, group);
-                               } catch (dcp::KDMFormatError& e) {
-                                       error_dialog (
-                                               this,
-                                               _("Could not read file as a KDM.  Perhaps it is badly formatted, or not a KDM at all."),
-                                               std_to_wx(e.what())
-                                               );
-                                       return;
-                               } catch (dcp::KDMDecryptionError &) {
-                                       error_dialog (
-                                               this,
-                                               _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
-                                               );
-                               } catch (dcp::MiscError& e) {
-                                       error_dialog (
-                                               this,
-                                               _("Could not read file as a KDM.  It is much too large.  Make sure you are loading a DKDM (XML) file."),
-                                               std_to_wx(e.what())
-                                               );
+               ScopeGuard sg = [d]() { d->Destroy(); };
+
+               if (d->ShowModal() != wxID_OK) {
+                       return;
+               }
+
+               auto chain = Config::instance()->decryption_chain();
+               DCPOMATIC_ASSERT (chain->key());
+
+               wxArrayString paths;
+               d->GetPaths(paths);
+               for (unsigned int i = 0; i < paths.GetCount(); ++i) {
+                       try {
+                               dcp::EncryptedKDM ekdm(dcp::file_to_string(wx_to_std(paths[i]), MAX_KDM_SIZE));
+                               /* Decrypt the DKDM to make sure that we can */
+                               dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
+
+                               auto new_dkdm = make_shared<DKDM>(ekdm);
+                               auto group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm());
+                               if (!group) {
+                                       group = Config::instance()->dkdms ();
                                }
+                               add_dkdm(new_dkdm, group);
+                       } catch (dcp::KDMFormatError& e) {
+                               error_dialog (
+                                       this,
+                                       _("Could not read file as a KDM.  Perhaps it is badly formatted, or not a KDM at all."),
+                                       std_to_wx(e.what())
+                                       );
+                               return;
+                       } catch (dcp::KDMDecryptionError &) {
+                               error_dialog (
+                                       this,
+                                       _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
+                                       );
+                       } catch (dcp::MiscError& e) {
+                               error_dialog (
+                                       this,
+                                       _("Could not read file as a KDM.  It is much too large.  Make sure you are loading a DKDM (XML) file."),
+                                       std_to_wx(e.what())
+                                       );
                        }
-                       update_dkdm_view();
                }
-               d->Destroy ();
+               update_dkdm_view();
        }
 
        void add_dkdm_folder_clicked ()