I have a code block that looks like this:
using (MemoryStream outputZip = new MemoryStream())
{
using (ZipOutputStream zipstream = new ZipOutputStream(outputZip))
{
zipstream.Password = req.password;
zipstream.SetLevel(3);
foreach (var doc in loadocs)
{
ZipEntry entry = new ZipEntry(doc.FileName);
zipstream.PutNextEntry(entry);
zipstream.Write(doc.FileData, 0, doc.FileData.Length);
}
zipstream.CloseEntry();
zipstream.IsStreamOwner = false;
}
response.result = new Document()
{
name = doc1.Custodian + DateTime.Now.Date.ToString().Replace('/', '_').Replace(':', '_') + ".zip",
content = outputZip.ToArray()
};
}
I keep getting back
CWE 404 - Improper resource shutdown or release
pointing to the first line. Shouldn't the using statement take care of disposing of the object when it is completed? I can't seem to mitigate this error no matter what I do. I have tried to wrap everything up in try/catch/finally
and dispose of outputZip
in the finally but same error results pointing to declaration of outputZip
.
I have updated my code to look like this:
MemoryStream zipOutput = new MemoryStream();
try
{
ZipOutputStream zipstream = new ZipOutputStream(zipOutput);
try
{
}
catch (Exception ex)
{
}
finally
{
zipstream = null;
zipstream.Close();
}
}
catch(Exception ex)
{
}
finally
{
zipOutput = null;
zipOutput.Close();
}
I am still seeing the Improper resource shutdown or release on the MemoryStream. Although very redundant doesn't this code actually release the resource?