From 5f00237e49f459dba1bdff08c067607c4d481fe9 Mon Sep 17 00:00:00 2001
From: Jonathan Baecker <jonbae77@gmail.com>
Date: Tue, 2 Dec 2014 15:41:06 +0100
Subject: [PATCH] fix device_list and COM initialization failed in decklink_common

Signed-off-by: Jonathan Baecker <jonbae77@gmail.com>
---
 libavdevice/decklink_common.cpp | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/libavdevice/decklink_common.cpp b/libavdevice/decklink_common.cpp
index 9a9e44b..15a340a 100644
--- a/libavdevice/decklink_common.cpp
+++ b/libavdevice/decklink_common.cpp
@@ -42,16 +42,21 @@ IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
 {
     IDeckLinkIterator *iter;
 
-    if (CoInitialize(NULL) != S_OK) {
-        av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
-        return NULL;
-    }
-
-    if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
-                         IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
-        av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
-        return NULL;
-    }
+    HRESULT                  result;
+   // Initialize COM on this thread
+   result = CoInitialize(NULL);
+   if (FAILED(result))
+   {
+      fprintf(stderr, "Initialization of COM failed - result = %08x.\n", result);
+      return NULL;
+   }
+   // Create an IDeckLinkIterator object to enumerate all DeckLink cards in the system
+   result = CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**)&iter);
+   if (FAILED(result))
+   {
+      fprintf(stderr, "A DeckLink iterator could not be created.  The DeckLink drivers may not be installed.\n");
+      return NULL;
+   }
 
     return iter;
 }
@@ -69,9 +74,12 @@ static char *dup_wchar_to_utf8(wchar_t *w)
 }
 #define DECKLINK_STR    OLECHAR *
 #define DECKLINK_STRDUP dup_wchar_to_utf8
+#define DECKLINK_FREE(s) SysFreeString(s)
 #else
 #define DECKLINK_STR    const char *
 #define DECKLINK_STRDUP av_strdup
+/* free() is needed for a string returned by the DeckLink SDL. */
+#define DECKLINK_FREE(s) free((void *) s)
 #endif
 
 HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
@@ -81,8 +89,7 @@ HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
     if (hr != S_OK)
         return hr;
     *displayName = DECKLINK_STRDUP(tmpDisplayName);
-    /* free() is needed for a string returned by the DeckLink SDL. */
-    free((void *) tmpDisplayName);
+	DECKLINK_FREE(tmpDisplayName);
     return hr;
 }
 
-- 
2.2.0

