473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Object not closing

I have a zipfile that I am trying to close because the file is corrupted and
I get an error on it:

Exception of type 'java.util.zip.ZipException' was thrown

So I handle the error and finally try to close the error. But there appears
to be some time of timing issue.

ZipFile zipfile = null;

try
{
zipfile = new ZipFile(zipFileName);
}
catch(Exception exc)
{
throw new Exception("File: " + zipFileName + " Corrupted",
exc);
}
finally
{
if (zipfile != null)
{
zipfile.close();
}
}

In the above code, I get the exception and get the if (zipfile != null) test
and this is where it all takes a different path depending on how long I
take.

If the "if" test was not there, I would get an error saying that the object
does not exist and I will then get an error saying that I can't move it
because it is in use. If I trace through it, it also doesn't get to the
zipfile.close(), but it allows me to move it so it is obviously not in use.

How can I handle this? I can't use a "using" clause as it is not
IDisposable.

It seems to close if I step through which seems to allow it to get released.

Thanks,

Tom
Oct 1 '08 #1
5 1478
I found the problem (and the solution). The problem was that the
constructor is opening the .zip file passed and if it determines that the
file has a problem, it does an IOException but never closes the stream.

You have to wait until the Garbage Collector realizes that this object (the
..zip file) now has nothing pointing at it and closes it. But the problem is
you would never know when it was done.

So you do a System.GC.Collect() to force the Garbage Collector to do its
thing now. Now, there is no access problem.

It was really a couple line change:

ZipFile zipfile = null;

try
{
zipfile = new ZipFile(zipFileName);
}
catch(Exception exc)
{
throw new Exception("File: " + zipFileName + " Corrupted",
exc);
}
finally
{
if (zipfile != null)
{
zipfile.close();
}
else
System.GC.Collect();
}

Works perfectly.

Tom
"tshad" <tf*@dslextreme.comwrote in message
news:uy*************@TK2MSFTNGP06.phx.gbl...
>I have a zipfile that I am trying to close because the file is corrupted
and I get an error on it:

Exception of type 'java.util.zip.ZipException' was thrown

So I handle the error and finally try to close the error. But there
appears to be some time of timing issue.

ZipFile zipfile = null;

try
{
zipfile = new ZipFile(zipFileName);
}
catch(Exception exc)
{
throw new Exception("File: " + zipFileName + " Corrupted",
exc);
}
finally
{
if (zipfile != null)
{
zipfile.close();
}
}

In the above code, I get the exception and get the if (zipfile != null)
test and this is where it all takes a different path depending on how long
I take.

If the "if" test was not there, I would get an error saying that the
object does not exist and I will then get an error saying that I can't
move it because it is in use. If I trace through it, it also doesn't get
to the zipfile.close(), but it allows me to move it so it is obviously not
in use.

How can I handle this? I can't use a "using" clause as it is not
IDisposable.

It seems to close if I step through which seems to allow it to get
released.

Thanks,

Tom

Oct 2 '08 #2
On Wed, 01 Oct 2008 18:00:00 -0700, tshad <tf*@dslextreme.comwrote:
I found the problem (and the solution). The problem was that the
constructor is opening the .zip file passed and if it determines that the
file has a problem, it does an IOException but never closes the stream.

You have to wait until the Garbage Collector realizes that this object
(the
.zip file) now has nothing pointing at it and closes it. But the
problem is
you would never know when it was done.

So you do a System.GC.Collect() to force the Garbage Collector to do its
thing now. Now, there is no access problem.

It was really a couple line change:

[...]

Works perfectly.
Well, except for the fact that you're calling a method that you should
never need to call.

From your post, it looks like you're trying to use some Java library thing
from C#. My advice would be to ditch the buggy library. If they got that
wrong, who knows what other bugs you're going to run into.

Pete
Oct 2 '08 #3

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 01 Oct 2008 18:00:00 -0700, tshad <tf*@dslextreme.comwrote:
>I found the problem (and the solution). The problem was that the
constructor is opening the .zip file passed and if it determines that the
file has a problem, it does an IOException but never closes the stream.

You have to wait until the Garbage Collector realizes that this object
(the
.zip file) now has nothing pointing at it and closes it. But the
problem is
you would never know when it was done.

So you do a System.GC.Collect() to force the Garbage Collector to do its
thing now. Now, there is no access problem.

It was really a couple line change:

[...]

Works perfectly.

Well, except for the fact that you're calling a method that you should
never need to call.

From your post, it looks like you're trying to use some Java library thing
from C#. My advice would be to ditch the buggy library. If they got that
wrong, who knows what other bugs you're going to run into.
It is a Java library that is part of MS libraries. You have to reference
it, but it works well except for this problem. It was the only library I
could find that dealt with zip files and is pretty simple to use.

Thanks,

Tom
>
Pete

Oct 2 '08 #4
On Oct 2, 4:01*pm, "tshad" <t...@dslextreme.comwrote:
Well, except for the fact that you're calling a method that you should
never need to call.
From your post, it looks like you're trying to use some Java library thing
fromC#. *My advice would be to ditch the buggy library. *If they got that
wrong, who knows what other bugs you're going to run into.

It is a Java library that is part of MS libraries. *You have to reference
it, but it works well except for this problem. *It was the only libraryI
could find that dealt withzipfiles and is pretty simple to use.
Tom, have you checked out DotNetZip? It's free, much simpler to use,
(yes, it implements IDisposable), and doesn't require you to call
GC.Collect().
http://www.codeplex.com/DotNetZip .
Oct 4 '08 #5

"Cheeso" <dp******@mailinator.comwrote in message
news:49**********************************@k36g2000 pri.googlegroups.com...
On Oct 2, 4:01 pm, "tshad" <t...@dslextreme.comwrote:
Well, except for the fact that you're calling a method that you should
never need to call.
From your post, it looks like you're trying to use some Java library
thing
fromC#. My advice would be to ditch the buggy library. If they got that
wrong, who knows what other bugs you're going to run into.

It is a Java library that is part of MS libraries. You have to reference
it, but it works well except for this problem. It was the only library I
could find that dealt withzipfiles and is pretty simple to use.
Tom, have you checked out DotNetZip? It's free, much simpler to use,
(yes, it implements IDisposable), and doesn't require you to call
GC.Collect().
http://www.codeplex.com/DotNetZip .
I probably will look at this for my next project.

I already have this one working and this has been my only problem with it.
When I was looking before for how to handle zip files I don't think I saw
this one or probably would have used it. This was part of the .net
libraries (even though it was in Java) and did the job when I needed it.

I agree, I would rather not have to call GC.Collect(). But it isn't the
worse thing that could happen, I suppose.

Thanks,

Tom
Oct 6 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher W. Douglas | last post by:
I am working on a project in VB.NET using Visual Studio.NET 2003. I use dialog windows to get user information, and throw exceptions when there are user errors, such as leaving a field blank or...
0
by: Steve Jorgensen | last post by:
Hi all, I had just participating in a small thread here about when to set recordset variables to Nothing when I ran into an interesting case in my own code that highlighted the need for a...
3
by: W Akthar | last post by:
Hi I am trying to create a windows service which queries SQL Server on timed intervals and depending on the results send appointments to Outlook. The problem lies when I try to create an...
8
by: Lee Jackson | last post by:
Whats the difference between the following in terms of how the CLR allocates and garbage collects? Which is better in terms of memory usage/performance? a) foreach (string strURL in...
6
by: TZ | last post by:
I have two classes, UIHousehold and WIZEditHousehold (both are winforms). UIHousehold creates and shows the WIZEditHousehold (WIZEditHousehold can't be modal, so UIHousehold doesn't know when...
8
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it...
2
by: Dave | last post by:
I'm having trouble understanding dispose. I set up a class that, among other things, displays the time in a status bar panel. It does this by starting a thread. I create an instance of this...
1
by: Brian Bischof | last post by:
In my Win app I found that using the SMTP object doesn't release resources after closing the application. During testing I have to open and close the app many times and I found that after about three...
15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
35
by: Jamey Shuemaker | last post by:
I've seen multiple threads (several in the last 6 months or so) on this topic, but I wanted to clarify my practices. I understand the need for cleaning object variables by setting them to Nothing...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.