# EcwRasterLayer - file is locked after closing

**URL:** <https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163>\
**Category:** Services\
**Created:** [September 26, 2011, 1:14am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163 "2011-09-26T01:14:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil1](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/neil1/32/24972_2.png) [@Neil1](https://community.thinkgeo.com/u/Neil1)\
**Post date:** [September 26, 2011, 1:14am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/1 "2011-09-26T01:14:23Z")

</div>

I'm&nbsp;using&nbsp;version&nbsp;5.0.0.0&nbsp;of&nbsp;the&nbsp;services&nbsp;edition&nbsp;to&nbsp;prototype&nbsp;at&nbsp;the&nbsp;moment.&nbsp;&nbsp;In&nbsp;testing&nbsp;some&nbsp;of&nbsp;the&nbsp;prototype&nbsp;functionality&nbsp;which&nbsp;checks&nbsp;to&nbsp;see&nbsp;if&nbsp;the&nbsp;server&nbsp;application&nbsp;can&nbsp;open&nbsp;a&nbsp;raster&nbsp;layer&nbsp;(in&nbsp;this&nbsp;case&nbsp;an&nbsp;ECW)&nbsp;the&nbsp;file&nbsp;does&nbsp;not&nbsp;seem&nbsp;to&nbsp;be&nbsp;unlocked&nbsp;when&nbsp;the&nbsp;layer&nbsp;is&nbsp;closed.&nbsp;

In&nbsp;regard&nbsp;to&nbsp;the&nbsp;below&nbsp;code,&nbsp;have&nbsp;I&nbsp;missed&nbsp;anything?&nbsp;&nbsp;RasterLayer&nbsp;etc&nbsp;don't&nbsp;inherit&nbsp;from&nbsp;IDisposable&nbsp;so&nbsp;that's&nbsp;not&nbsp;an&nbsp;option.&nbsp;&nbsp;We'll&nbsp;be&nbsp;doing&nbsp;this&nbsp;same&nbsp;sort&nbsp;of&nbsp;thing&nbsp;with&nbsp;a&nbsp;number&nbsp;of&nbsp;other&nbsp;raster&nbsp;layer&nbsp;types&nbsp;as&nbsp;well.&nbsp;

The&nbsp;below&nbsp;code&nbsp;exhibits&nbsp;the&nbsp;issue:

`public void TestLockedFile() { var filePath = Path.Combine(m_currentPath, Guid.NewGuid().ToString() + ".ecw"); try { File.WriteAllBytes(filePath, Resources.testECW); var layer = new EcwRasterLayer(filePath); } finally { File.Delete(filePath); // this works fine } try { File.WriteAllBytes(filePath, Resources.testECW); var layer = new EcwRasterLayer(filePath); layer.Open(); layer.Close(); } finally { File.Delete(filePath); // this fails as the file is locked by another process } } `&nbsp;  
  
Any&nbsp;thoughts?

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [September 27, 2011, 10:34am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/2 "2011-09-27T10:34:48Z")

</div>

Neil,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;have&nbsp;recreated&nbsp;your&nbsp;problem,&nbsp;but&nbsp;don’t&nbsp;find&nbsp;solution&nbsp;so&nbsp;far.&nbsp;Just&nbsp;share&nbsp;more&nbsp;information&nbsp;to&nbsp;you,&nbsp;maybe&nbsp;you&nbsp;have&nbsp;some&nbsp;idea&nbsp;to&nbsp;help&nbsp;us&nbsp;solve&nbsp;it.&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;The&nbsp;Open&nbsp;method&nbsp;do&nbsp;following&nbsp;thing:&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rasterImage&nbsp;=&nbsp;new&nbsp;GisImage();&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rasterImage.LoadFile(imagePathFilename);&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;worldFile&nbsp;=&nbsp;GetImageWorldFile();&nbsp;  
&nbsp;&nbsp;  
&nbsp;The&nbsp;Close&nbsp;method&nbsp;do&nbsp;following&nbsp;thing:&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rasterImage&nbsp;=&nbsp;null;&nbsp;  
&nbsp;&nbsp;  
&nbsp;So&nbsp;I&nbsp;think&nbsp;in&nbsp;Close&nbsp;method,&nbsp;it&nbsp;should&nbsp;done&nbsp;more&nbsp;things&nbsp;to&nbsp;dispose&nbsp;object.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;James

---

<div class="post-metadata">

**Author:** ![Neil1](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/neil1/32/24972_2.png) [@Neil1](https://community.thinkgeo.com/u/Neil1)\
**Post date:** [September 27, 2011, 11:00pm UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/3 "2011-09-27T23:00:56Z")

</div>

James,

Thanks&nbsp;for&nbsp;the&nbsp;reply.&nbsp;&nbsp;I'm&nbsp;guessing&nbsp;the&nbsp;GisImage&nbsp;is&nbsp;one&nbsp;of&nbsp;the&nbsp;internal&nbsp;classes&nbsp;that&nbsp;I&nbsp;can't&nbsp;see&nbsp;nor&nbsp;have&nbsp;access&nbsp;to.

But&nbsp;I&nbsp;guess&nbsp;in&nbsp;general&nbsp;terms&nbsp;it&nbsp;must&nbsp;be&nbsp;holding&nbsp;a&nbsp;handle&nbsp;open&nbsp;on&nbsp;the&nbsp;file&nbsp;until&nbsp;it&nbsp;is&nbsp;garbage&nbsp;collected&nbsp;(or&nbsp;the&nbsp;application&nbsp;terminates&nbsp;if&nbsp;there&nbsp;is&nbsp;no&nbsp;finalizer).&nbsp;&nbsp;I&nbsp;would&nbsp;be&nbsp;expecting&nbsp;that&nbsp;the&nbsp;close&nbsp;method&nbsp;would&nbsp;call&nbsp;a&nbsp;method&nbsp;on&nbsp;the&nbsp;image&nbsp;(like&nbsp;close&nbsp;or&nbsp;dispose)&nbsp;that&nbsp;would&nbsp;release&nbsp;that&nbsp;handle/close&nbsp;the&nbsp;stream.

I&nbsp;had&nbsp;also&nbsp;added&nbsp;a&nbsp;GC.Collect()&nbsp;previously&nbsp;into&nbsp;the&nbsp;test&nbsp;to&nbsp;see&nbsp;if&nbsp;that&nbsp;would&nbsp;release&nbsp;the&nbsp;file&nbsp;-&nbsp;but&nbsp;it&nbsp;didn't&nbsp;either.&nbsp;&nbsp;Not&nbsp;that&nbsp;I&nbsp;would&nbsp;want&nbsp;to&nbsp;do&nbsp;this&nbsp;in&nbsp;production&nbsp;code.

I&nbsp;can't&nbsp;really&nbsp;be&nbsp;of&nbsp;much&nbsp;more&nbsp;help&nbsp;-&nbsp;the&nbsp;implication&nbsp;that&nbsp;I'm&nbsp;deriving&nbsp;from&nbsp;this&nbsp;is&nbsp;that&nbsp;I&nbsp;would&nbsp;have&nbsp;to&nbsp;stop&nbsp;the&nbsp;application&nbsp;generating&nbsp;the&nbsp;map&nbsp;tiles&nbsp;if&nbsp;it&nbsp;was&nbsp;using&nbsp;an&nbsp;ECW&nbsp;to&nbsp;update&nbsp;the&nbsp;ECW.&nbsp;&nbsp;Now&nbsp;maybe&nbsp;this&nbsp;needs&nbsp;to&nbsp;be&nbsp;done&nbsp;for&nbsp;speed&nbsp;and&nbsp;caching&nbsp;considerations?&nbsp;&nbsp;In&nbsp;this&nbsp;case&nbsp;it&nbsp;would&nbsp;still&nbsp;be&nbsp;nice&nbsp;to&nbsp;have&nbsp;a&nbsp;method&nbsp;that&nbsp;would&nbsp;actually&nbsp;close&nbsp;access&nbsp;to&nbsp;the&nbsp;file.

Neil.

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [September 28, 2011, 11:21am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/4 "2011-09-28T11:21:58Z")

</div>

Neil,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;for&nbsp;your&nbsp;information,&nbsp;I&nbsp;am&nbsp;still&nbsp;looking&nbsp;for&nbsp;solution,&nbsp;and&nbsp;also&nbsp;I&nbsp;sent&nbsp;email&nbsp;to&nbsp;you&nbsp;about&nbsp;detail&nbsp;for&nbsp;GisImage.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Let&nbsp;me&nbsp;know&nbsp;if&nbsp;you&nbsp;have&nbsp;any&nbsp;new&nbsp;ideas.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;James

---

<div class="post-metadata">

**Author:** ![Neil1](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/neil1/32/24972_2.png) [@Neil1](https://community.thinkgeo.com/u/Neil1)\
**Post date:** [September 28, 2011, 6:51pm UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/5 "2011-09-28T18:51:32Z")

</div>

James,

I've&nbsp;replied&nbsp;to&nbsp;your&nbsp;email&nbsp;with&nbsp;some&nbsp;more&nbsp;information&nbsp;-&nbsp;of&nbsp;where&nbsp;I&nbsp;think&nbsp;the&nbsp;problem&nbsp;actually&nbsp;is.

Neil.

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [September 30, 2011, 9:20am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/6 "2011-09-30T09:20:53Z")

</div>

Neil,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;have&nbsp;tried&nbsp;to&nbsp;implement&nbsp;your&nbsp;code&nbsp;to&nbsp;GisImage,&nbsp;but&nbsp;still&nbsp;has&nbsp;the&nbsp;same&nbsp;problem,&nbsp;it&nbsp;seems&nbsp;the&nbsp;code&nbsp;below&nbsp;couldn’t&nbsp;really&nbsp;dispose&nbsp;the&nbsp;file&nbsp;handler&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;get&nbsp;and&nbsp;explcitly&nbsp;invoke&nbsp;the&nbsp;finalizer&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;finalizeMethod&nbsp;=&nbsp;loadedGeoRasterWrapper.GetType().GetMethod(“Finalize”,&nbsp;BindingFlags.Instance&nbsp;|&nbsp;BindingFlags.NonPublic);&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finalizeMethod.Invoke(loadedGeoRasterWrapper,&nbsp;null);&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GC.SuppressFinalize(loadedGeoRasterWrapper);&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;will&nbsp;keep&nbsp;researching&nbsp;on&nbsp;the&nbsp;solution,&nbsp;any&nbsp;progress&nbsp;I&nbsp;will&nbsp;update&nbsp;to&nbsp;you,&nbsp;thanks&nbsp;for&nbsp;your&nbsp;help.&nbsp;  
&nbsp;&nbsp;  
&nbsp;James

---

<div class="post-metadata">

**Author:** ![Neil1](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/neil1/32/24972_2.png) [@Neil1](https://community.thinkgeo.com/u/Neil1)\
**Post date:** [October 3, 2011, 7:13am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/7 "2011-10-03T07:13:51Z")

</div>

James,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I’m&nbsp;a&nbsp;little&nbsp;surprised&nbsp;I&nbsp;had&nbsp;a&nbsp;couple&nbsp;of&nbsp;my&nbsp;colleagues&nbsp;run&nbsp;the&nbsp;same&nbsp;test&nbsp;code&nbsp;on&nbsp;their&nbsp;machines&nbsp;and&nbsp;it&nbsp;works&nbsp;ok.&nbsp;&nbsp;We&nbsp;are&nbsp;all&nbsp;running&nbsp;x64&nbsp;machines&nbsp;if&nbsp;that&nbsp;makes&nbsp;any&nbsp;difference.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Neil.

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [October 8, 2011, 2:12am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/8 "2011-10-08T02:12:44Z")

</div>

Neil,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Maybe&nbsp;my&nbsp;environment&nbsp;has&nbsp;problem,&nbsp;any&nbsp;way&nbsp;I&nbsp;have&nbsp;implemented&nbsp;it&nbsp;to&nbsp;our&nbsp;core,&nbsp;you&nbsp;can&nbsp;get&nbsp;the&nbsp;latest&nbsp;development&nbsp;daily&nbsp;build&nbsp;(5.0.150.0)&nbsp;and&nbsp;have&nbsp;another&nbsp;try.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;James&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![Neil1](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/neil1/32/24972_2.png) [@Neil1](https://community.thinkgeo.com/u/Neil1)\
**Post date:** [October 8, 2011, 7:36am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/9 "2011-10-08T07:36:10Z")

</div>

Thanks&nbsp;James,&nbsp;will&nbsp;try&nbsp;it&nbsp;out&nbsp;when&nbsp;it's&nbsp;available&nbsp;and&nbsp;let&nbsp;you&nbsp;know.

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [October 9, 2011, 3:00am UTC](https://community.thinkgeo.com/t/ecwrasterlayer-file-is-locked-after-closing/7163/10 "2011-10-09T03:00:38Z")

</div>

You’re&nbsp;welcome,&nbsp;Neil.
