diff --git a/magick/cache.c b/magick/cache.c
index 80b7466..2794b15 100644
--- a/magick/cache.c
+++ b/magick/cache.c
@@ -184,6 +184,7 @@
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   (void) ResetMagickMemory(cache_info,0,sizeof(*cache_info));
   cache_info->type=UndefinedCache;
+  cache_info->mode=IOMode;
   cache_info->colorspace=RGBColorspace;
   cache_info->file=(-1);
   cache_info->id=GetMagickThreadId();
@@ -3910,6 +3911,7 @@
   source_info.file=(-1);
   (void) FormatMagickString(cache_info->filename,MaxTextExtent,"%s[%ld]",
     image->filename,GetImageIndexInList(image));
+  cache_info->mode=mode;
   cache_info->rows=image->rows;
   cache_info->columns=image->columns;
   cache_info->active_index_channel=((image->storage_class == PseudoClass) ||
@@ -4169,13 +4171,13 @@
             {
               (void) CopyMagickString(cache_info->cache_filename,filename,
                 MaxTextExtent);
-              cache_info=(CacheInfo *) ReferencePixelCache(cache_info);
               *offset+=cache_info->length+pagesize-(cache_info->length %
                 pagesize);
+              (void) UnlockSemaphoreInfo(cache_info->semaphore);
+              cache_info=(CacheInfo *) ReferencePixelCache(cache_info);
               if (image->debug != MagickFalse)
                 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
                   "Usurp resident persistent cache");
-              (void) UnlockSemaphoreInfo(cache_info->semaphore);
               return(MagickTrue);
             }
         }