OpenCL will be disabled when the cache directory cannot be determined.
diff --git a/MagickCore/opencl.c b/MagickCore/opencl.c
index 462320f..ac4c56a 100644
--- a/MagickCore/opencl.c
+++ b/MagickCore/opencl.c
@@ -268,81 +268,51 @@
 
 static const char *GetOpenCLCacheDirectory()
 {
-  if (cache_directory != (char *) NULL)
-    return(cache_directory);
-
-  if (cache_directory_lock == (SemaphoreInfo *) NULL)
-    ActivateSemaphoreInfo(&cache_directory_lock);
-  LockSemaphoreInfo(cache_directory_lock);
   if (cache_directory == (char *) NULL)
     {
-      char
-        *home,
-        path[MagickPathExtent],
-        *temp;
-
-      MagickBooleanType
-        status;
-
-      struct stat
-        attributes;
-
-      home=GetEnvironmentValue("MAGICK_OPENCL_CACHE_DIR");
-      if (home == (char *) NULL)
+      if (cache_directory_lock == (SemaphoreInfo *) NULL)
+        ActivateSemaphoreInfo(&cache_directory_lock);
+      LockSemaphoreInfo(cache_directory_lock);
+      if (cache_directory == (char *) NULL)
         {
-          home=GetEnvironmentValue("XDG_CACHE_HOME");
-          if (home == (char *) NULL)
-            home=GetEnvironmentValue("LOCALAPPDATA");
-          if (home == (char *) NULL)
-            home=GetEnvironmentValue("APPDATA");
-          if (home == (char *) NULL)
-            home=GetEnvironmentValue("USERPROFILE");
-        }
+          char
+            *home,
+            path[MagickPathExtent],
+            *temp;
 
-      if (home != (char *) NULL)
-        {
-          /* first check if $HOME exists */
-          (void) FormatLocaleString(path,MagickPathExtent,"%s",home);
-          status=GetPathAttributes(path,&attributes);
-          if (status == MagickFalse)
-            status=MagickCreateDirectory(path);
+          MagickBooleanType
+            status;
 
-          /* first check if $HOME/ImageMagick exists */
-          if (status != MagickFalse)
+          struct stat
+            attributes;
+
+          temp=(char *) NULL;
+          home=GetEnvironmentValue("MAGICK_OPENCL_CACHE_DIR");
+          if (home == (char *) NULL)
             {
-              (void) FormatLocaleString(path,MagickPathExtent,
-                "%s%sImageMagick",home,DirectorySeparator);
-
-              status=GetPathAttributes(path,&attributes);
-              if (status == MagickFalse)
-                status=MagickCreateDirectory(path);
+              home=GetEnvironmentValue("XDG_CACHE_HOME");
+              if (home == (char *) NULL)
+                home=GetEnvironmentValue("LOCALAPPDATA");
+              if (home == (char *) NULL)
+                home=GetEnvironmentValue("APPDATA");
+              if (home == (char *) NULL)
+                home=GetEnvironmentValue("USERPROFILE");
             }
 
-          if (status != MagickFalse)
-            {
-              temp=(char*)AcquireMagickMemory(strlen(path)+1);
-              CopyMagickString(temp,path,strlen(path)+1);
-            }
-          home=DestroyString(home);
-        }
-      else
-        {
-          home=GetEnvironmentValue("HOME");
           if (home != (char *) NULL)
             {
-              /* first check if $HOME/.cache exists */
-              (void) FormatLocaleString(path,MagickPathExtent,"%s%s.cache",
-                home,DirectorySeparator);
+              /* first check if $HOME exists */
+              (void) FormatLocaleString(path,MagickPathExtent,"%s",home);
               status=GetPathAttributes(path,&attributes);
               if (status == MagickFalse)
                 status=MagickCreateDirectory(path);
 
-              /* first check if $HOME/.cache/ImageMagick exists */
+              /* first check if $HOME/ImageMagick exists */
               if (status != MagickFalse)
                 {
                   (void) FormatLocaleString(path,MagickPathExtent,
-                    "%s%s.cache%sImageMagick",home,DirectorySeparator,
-                    DirectorySeparator);
+                    "%s%sImageMagick",home,DirectorySeparator);
+
                   status=GetPathAttributes(path,&attributes);
                   if (status == MagickFalse)
                     status=MagickCreateDirectory(path);
@@ -350,15 +320,50 @@
 
               if (status != MagickFalse)
                 {
-                  temp=(char*)AcquireMagickMemory(strlen(path)+1);
+                  temp=(char*) AcquireMagickMemory(strlen(path)+1);
                   CopyMagickString(temp,path,strlen(path)+1);
                 }
               home=DestroyString(home);
             }
+          else
+            {
+              home=GetEnvironmentValue("HOME");
+              if (home != (char *) NULL)
+                {
+                  /* first check if $HOME/.cache exists */
+                  (void) FormatLocaleString(path,MagickPathExtent,"%s%s.cache",
+                    home,DirectorySeparator);
+                  status=GetPathAttributes(path,&attributes);
+                  if (status == MagickFalse)
+                    status=MagickCreateDirectory(path);
+
+                  /* first check if $HOME/.cache/ImageMagick exists */
+                  if (status != MagickFalse)
+                    {
+                      (void) FormatLocaleString(path,MagickPathExtent,
+                        "%s%s.cache%sImageMagick",home,DirectorySeparator,
+                        DirectorySeparator);
+                      status=GetPathAttributes(path,&attributes);
+                      if (status == MagickFalse)
+                        status=MagickCreateDirectory(path);
+                    }
+
+                  if (status != MagickFalse)
+                    {
+                      temp=(char*) AcquireMagickMemory(strlen(path)+1);
+                      CopyMagickString(temp,path,strlen(path)+1);
+                    }
+                  home=DestroyString(home);
+                }
+            }
+          if (temp == (char *) NULL)
+            temp=AcquireString("?");
+          cache_directory=temp;
         }
-      cache_directory=temp;
+      UnlockSemaphoreInfo(cache_directory_lock);
     }
-  UnlockSemaphoreInfo(cache_directory_lock);
+  if (*cache_directory == '?')
+    return((const char *) NULL);
   return(cache_directory);
 }
 
@@ -1459,6 +1464,8 @@
     j;
 
   clEnv=GetCurrentOpenCLEnv();
+  if (clEnv == (MagickCLEnv) NULL)
+    return;
 
   for (i = 0; i < clEnv->number_devices; i++)
     if (clEnv->devices[i]->profile_kernels != MagickFalse)
@@ -1676,6 +1683,9 @@
       return(default_CLEnv);
   }
 
+  if (GetOpenCLCacheDirectory() == (char *) NULL)
+    return((MagickCLEnv) NULL);
+
   if (openCL_lock == (SemaphoreInfo *) NULL)
     ActivateSemaphoreInfo(&openCL_lock);