K
Koliber (js)
I feel i still do not understand maybe a bit a dispose pattern
So I have a question - is this code right? Is fs.Close() there where
it is right?
If I do understand it in place 'BBB' there can be a situation where
such member
obiect as fs can be finalised - so it may be wrong. Am I right or not?
But if i would close fs in place 'AAA' then I am not sure if a user of
this class
called a dispose() on it ? Am I wrong or right?
Thanks for Answer
JS
class JpgFileDecoder : IDisposable
{
private FileStream fs = null;
private ProgressEventHandler progressEventHandler = null;
private JpegDecoder jpegDecoder = null;
private AtalaImage atalaImage = null;
private ImageInfo imageInfo = null;
private bool isDisposed = false;
public JpgFileDecoder(string filePath,
ProgressEventHandler progressEventHandler)
{
this.progressEventHandler = progressEventHandler;
fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
}
public AtalaImage AtalaImage
{
get
{
if (atalaImage == null)
{
atalaImage = jpegDecoder.Read(fs,
progressEventHandler);
}
return atalaImage;
}
set { }
}
public ImageInfo ImageInfo
{
get
{
if (imageInfo == null)
{
imageInfo = jpegDecoder.GetImageInfo(fs);
}
return imageInfo;
}
set { }
}
~JpgFileDecoder()
{
Dispose(false);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// AAA
// Code to dispose the managed resources of the class
}
//BBB
// Code to dispose the un-managed resources of the
class
fs.Close();
fs = null;
//
isDisposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
So I have a question - is this code right? Is fs.Close() there where
it is right?
If I do understand it in place 'BBB' there can be a situation where
such member
obiect as fs can be finalised - so it may be wrong. Am I right or not?
But if i would close fs in place 'AAA' then I am not sure if a user of
this class
called a dispose() on it ? Am I wrong or right?
Thanks for Answer
JS
class JpgFileDecoder : IDisposable
{
private FileStream fs = null;
private ProgressEventHandler progressEventHandler = null;
private JpegDecoder jpegDecoder = null;
private AtalaImage atalaImage = null;
private ImageInfo imageInfo = null;
private bool isDisposed = false;
public JpgFileDecoder(string filePath,
ProgressEventHandler progressEventHandler)
{
this.progressEventHandler = progressEventHandler;
fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
}
public AtalaImage AtalaImage
{
get
{
if (atalaImage == null)
{
atalaImage = jpegDecoder.Read(fs,
progressEventHandler);
}
return atalaImage;
}
set { }
}
public ImageInfo ImageInfo
{
get
{
if (imageInfo == null)
{
imageInfo = jpegDecoder.GetImageInfo(fs);
}
return imageInfo;
}
set { }
}
~JpgFileDecoder()
{
Dispose(false);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// AAA
// Code to dispose the managed resources of the class
}
//BBB
// Code to dispose the un-managed resources of the
class
fs.Close();
fs = null;
//
isDisposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}